1

我正在探索使用 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 适配器,我能够显示列表并创建新记录。但是,绑定到模板的新创建记录不会更新,除非我点击浏览器上的刷新按钮。现在我想知道我可能做错了什么。

在发布之前,我在 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>
4

1 回答 1

1

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

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

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

于 2013-09-11T16:29:54.107 回答