本地存储适配器没有方法'commit'

时间:2013-09-14 08:36:24

标签: ember.js local-storage ember-data

我创建了一个使用Local Storage Adapter的示例应用: hbs代码是:

  <script type="text/x-handlebars">
<h2>Welcome to Ember.js</h2>
<input type="button" {{action cr}} value="Create"/>
<ul>
{{#each item in model}}
<li>{{item.name}}</li>
{{else}}
NO Item
{{/each}}
<ul>
</script>

,app.js文件为:

App = Ember.Application.create();
App.LSAdapter = DS.LSAdapter.extend({
namespace: 'app'
});

App.ApplicationAdapter = DS.LSAdapter;

App.Router.map(function() {
});

App.Store = DS.Store.extend();
App.store = App.Store.create();
App.Item = DS.Model.extend({
name:DS.attr('string'),
uniqueName:DS.attr('string')
});
App.ApplicationRoute = Ember.Route.extend({
model:function(){
    return this.get('store').findAll('item');
}
});
App.IndexRoute = Ember.Route.extend({
model:function(){
    return this.get('store').findAll('item');
}
});
App.Item.reopen({
url:'localhost/app/'
});
App.ApplicationController = Ember.ArrayController.extend({
actions:{
cr:function(){
this.get('store').createRecord('item',{
    id:Math.random().toString(32).slice(2).substr(0, 5),
    name:'Hello',
    uniqueName:'Hello 2'
});
App.store.commit();
}
}
});

但是我收到了错误:

Uncaught TypeError: Object [object Object] has no method 'commit' 

我正在使用emberjs 1.0和最后一个ember数据build.i想要将记录保存到本地存储,我无法找到任何样本。

1 个答案:

答案 0 :(得分:2)

您无需明确创建Store,因此删除此行:

App.store = App.Store.create();

此外,在ApplicationController

中更改此设置
App.ApplicationController = Ember.ArrayController.extend({
  actions:{
    cr:function(){
      var item = this.get('store').createRecord('item',{
        id:Math.random().toString(32).slice(2).substr(0, 5),
        name:'Hello',
        uniqueName:'Hello 2'
      });
      item.save();
    }
  }
});

希望它有所帮助。