首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ember-model RESTAdapter时,绑定视图在保存时不会更新。

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

Stack Overflow用户
提问于 2013-07-08 13:02:06
回答 1查看 438关注 0票数 1

我正在探索在我的ember.js应用程序中使用成员模型(https://github.com/ebryn/ember-model)作为成员数据的替代方案。我还没有足够的代表来创建一个新的标签,但是如果有的话,我会创建ember-model.js来避免混淆。

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

这在ember-model内部的Fixture Adaptor中工作得很顺利。在创建时,正如预期的那样,新记录将被推送到Fixture数组中,并且浏览器会自动更新以反映新记录。

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

在我发帖之前,我在StackOverflow和其他地方翻了翻。接下来我将深入挖掘源代码,但我想我应该问问其他人是否有任何见解。

谢谢,Stack Overflow的好心人。

这是我的app.js:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<!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>
EN

回答 1

Stack Overflow用户

发布于 2013-09-12 00:29:54

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

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

您的应用程序控制器将是一个ArrayController,因为应用程序路由的模型函数返回一个数组。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17519469

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档