2013-07-19 70 views
1

型號的所有產品定義模型刪除使用Emberjs

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功能changeMarkets模型對象。 我想知道如何刪除模型的所有內容。

回答

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" 
     }) 
    ]); 
    } 
}); 

希望它有幫助。