使用ember-model RESTAdapter时,绑定视图在保存时不会更新。

时间:2013-07-08 05:02:06

标签: ember.js

我正在探索使用ember-model(https://github.com/ebryn/ember-model)作为ember.js应用程序中ember-data的替代方法。我还没有足够的代表创建一个新标签,但如果是这样,我会创建ember-model.js以避免混淆。

所需的行为是显示一个简单的记录列表,并显示一个表单,通过RESTful rails API,使用GET和POST创建新记录。

这与Ember-model内的Fixture Adapter顺利运行。按照预期创建后,新记录将被推送到Fixture数组,浏览器会自动更新以反映新记录。

使用ReST Adapter,我能够显示列表并创建新记录。但是,除非我点击浏览器上的刷新按钮,否则绑定到模板的新创建的记录将不会更新。而现在我想知道我可能做错了什么。

在我发布之前,我在StackOverflow和其他地方挖过。我接下来会深入挖掘资料来源,但我想我会问是否有其他人有任何见解。

谢谢你,Stack Overflow的好人。

这是我的app.js:

window.Dash = Ember.Application.create({
LOG_TRANSITIONS: true
});

Dash.Router.map(function() {
// put your routes here
this.resource('apps', function() {
    this.route('add');
    this.resource('app', { path: ':app_id' });

});
});

Dash.IndexRoute = Ember.Route.extend({
    //temporary redirect to games list
    redirect: function () {
    this.transitionTo('apps');
    }
});

Dash.AppsRoute = Ember.Route.extend({
    model: function() {
        return Dash.App.find();
    }
});

Dash.AppsAddController = Ember.Controller.extend({
    save: function() {
    var name = this.get('name');
    var id = this.get('id');
    var account_id = this.get('account_id');
    var auth_id = this.get('auth_id');
    var state = Dash.AppState.selectedState;
    var store_type = Dash.AppPlatform.selectedPlatform;
    var store_app_id = this.get('store_app_id');
    var download_url = this.get('download_url');

    var newApp = Dash.App.create({
        name: name,
        id: id,
        auth_id: auth_id,
        state: state,
        store_type: store_type,
        store_app_id: store_app_id,
        download_url: download_url
    });
    newApp.save();
}
});

Dash.AppPlatform = Ember.ArrayController.create({
    selectedPlatform: null,
    content: [
    Ember.Object.create({id: 'itunes', name: 'iOS'}),
    Ember.Object.create({id: 'android', name: 'Google Play'}),
        Ember.Object.create({id: 'amazon', name: 'Amazon Appstore'}),
    ] 
});

Dash.AppState = Ember.ArrayController.create({
selectedState: null,
content: [
    Ember.Object.create({name: 'test'}),
    Ember.Object.create({name: 'production'})
]
})


Dash.App = Ember.Model.extend({
   id: Ember.attr(),
   name: Ember.attr(),
   auth_id: Ember.attr(),
   state: Ember.attr(),
   store_type: Ember.attr(),
   store_app_id: Ember.attr(),
   download_url: Ember.attr(),
});

Dash.App.adapter = Ember.RESTAdapter.create();

//hardcoding account id for now
Dash.App.url = 'http://localhost:3000/accounts/980190962/apps';
Dash.App.collectionKey = 'apps';

这是index.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Dashboard</title>
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
  <h2>Dashboard</h2>
<script type="text/x-handlebars" data-template-name="application">
    {{outlet}}
</script>

<script type="text/x-handlebars" data-template-name="apps">
    {{#linkTo 'apps.add' }}Add an App{{/linkTo}}

    <h3>Apps List</h3>
    {{#if model}}
      <ol>
      {{#each model}}
        <li>{{#linkTo 'app' this}}{{name}}{{/linkTo}}</li>
      {{/each}} 
      </ol>

      <div>{{outlet}}</div>
    {{else}}
      <p>You have no Apps.</p>
    {{/if}}
  </script>

  <script type="text/x-handlebars" data-template-name="apps/index">
      <p>Click a game to Edit or click Add to enter a new App.</p>
  </script>

  <script type="text/x-handlebars" data-template-name="app">
    <div>
      Name: {{name}}<br />
      ID: {{id}}<br />
      Account ID: {{account_id}}<br />
      AuthID : {{auth_id}}<br />
      Test Mode?: {{state}}<br />    
      Store Type: {{store_type}}<br />
      Store App ID: {{store_app_id}}<br />
      Download URL: {{download_url}}
    </div>
  </script>

  <script type="text/x-handlebars" data-template-name="apps/add">
    <p><label for="name">Name:</label><br />{{view Ember.TextField valueBinding='name'}}</p>
    <p><label for="id">ID:</label><br />{{view Ember.TextField valueBinding='id'}}</p>
    <p><label for="account_id">Account ID:</label><br />{{view Ember.TextField     valueBinding='account_id'}}</p>
    <p><label for="auth_id">Auth ID:</label><br />{{view Ember.TextField valueBinding='auth_id'}}</p>
    <p><label for="state">Test Mode:</label><br />{{view Ember.Select     contentBinding='Dash.AppState' valueBinding='Dash.AppState.selectedState' optionValuePath="content.name" optionLabelPath="content.name"}}</p>
    <p><label for="store_type">Store Type:</label><br />{{view Ember.Select     contentBinding='Dash.AppPlatform' valueBinding='Dash.AppPlatform.selectedPlatform' optionValuePath="content.id" optionLabelPath="content.name"}}</p>
    <p><label for="store_app_id">Store App ID:</label><br />{{view Ember.TextField valueBinding='store_app_id'}}</p>
    <p><label for="download_url">Download URL:</label><br />{{view Ember.TextField valueBinding='download_url'}}</p>   
    <p><button {{action 'save'}}>Save</button></p>
  </script>

      <script src="js/libs/jquery-1.9.1.js"></script>
      <script src="js/libs/handlebars-1.0.0-rc.4.js"></script>
      <script src="js/libs/ember-1.0.0-rc.6.js"></script>
      <script src="js/libs/ember-model.js"></script>
      <script src="js/app.js"></script>

    </body>
    </html>

1 个答案:

答案 0 :(得分:1)

它不会自动以这种方式工作。 AppState控制器中的内容数组只是一个包含项目的静态数组。你必须做这样的事情

this.controllerFor('Apps')。get('content')。pushObject(newApp),看它反映在UI中。

您的Apps控制器将是一个ArrayController,因为Apps路由的模型函数返回了一个数组。