1

定义模型

App.Store = DS.Store.extend({
   revision: 12,
   adapter: DS.FixtureAdapter
});

App.Markets = DS.Model.extend({
    name: DS.attr("string"),
    created: DS.attr("string")
});

App.Markets.FIXTURES = [
    {id:1, name:"name1", created:'2013-1-1'},
    {id:2, name:"name2", created:'2013-1-2'},
    {id:3, name:"name3", created:'2013-1-3'}
]; 

定义控制器

App.MarketsController = Ember.ArrayController.extend({
    changeMarkets:function(marketer_id){
        // remove all item of App.Markets Model
        //-------

        // add new item in App.Markets Model
        App.Sources.createRecord({
            id: 100,
            name: "new name1",
            created: "2014-2-1"
        });

        App.Sources.createRecord({
            id: 200,
            name: "new name2",
            created: "2014-2-2"
        });

        App.Sources.createRecord({
            id: 300,
            name: "new name3",
            created: "2014-2-3"
        });
    }   
}); 

我将使用 emberjs 删除所有 itme 并添加一些到 changeMarkets 函数中的模型对象。我想知道如何删除模型的所有项目。

4

1 回答 1

2

简短的在这里查看一个工作示例:http: //jsbin.com/eyojit/5/edit

长:要从控制器content属性中删除所有项目,您可以使用内置方法clear(),这将提高性能并且您的绑定将正常工作。然后设置新项目,你可以这样做,这里是相关代码:

App.MarketsController = Ember.ArrayController.extend({
  changeMarkets:function(marketer_id){
    // remove all item of App.Markets Model
    this.get('content').clear();

    // add new item in App.Markets Model
    this.set('content', [
      App.Markets.createRecord({
        id: 100,
        name: "new name1",
        created: "2014-2-1"
      }),
      App.Markets.createRecord({
        id: 200,
        name: "new name2",
        created: "2014-2-2"
      }),
      App.Markets.createRecord({
        id: 300,
        name: "new name3",
        created: "2014-2-3"
      })
    ]);
  }   
});

希望能帮助到你。

于 2013-07-19T13:01:38.467 回答