我一直在四处寻找,我找不到任何最新的ember(1.0.0-rc.1)和ember-data(修订版11)也使用socket.io的例子.我尝试过这样的事情.
App.ApplicationRoute = Ember.Route.extend({
setupController: function(controller, data) {
var socket = io.connect(),
self = this;
socket.on('apartment/new', function(apartment) {
var apt = App.Apartment.createRecord(apartment);
self.controllerFor('apartments').pushObject(apt);
});
}
});
Run Code Online (Sandbox Code Playgroud)
这实际上将创建一个新的模型类,它将对象推送到控制器,并创建一个新的li但值不会呈现.
<ul class="list-view">
{{#each apartment in controller}}
<li>
{{#linkTo 'apartment' apartment }}
<span class="date">{{date apartment.date}}</span>
{{apartment.title}}
{{/linkTo}}
</li>
{{/each}}
</ul>
Run Code Online (Sandbox Code Playgroud)
这与运行循环有关吗?如何强制渲染值?或者有更好的方法吗?
有什么区别
// 'this' is the controller
this.get('model').save();
Run Code Online (Sandbox Code Playgroud)
和
// 'this' is the controller
this.get('model').get('store').commit();
Run Code Online (Sandbox Code Playgroud)
? 在我做的小测试中,他们都给了我相同的结果.我应该使用哪一个?
我调查了第一个,它调用了
DS.Model = Ember.Object.extend(
...
save: function() {
this.get('store').scheduleSave(this);
var promise = new Ember.RSVP.Promise();
this.one('didCommit', this, function() {
promise.resolve(this);
});
return promise;
},
Run Code Online (Sandbox Code Playgroud)
那么问题就变成了,this.get('store').scheduleSave(this)和之间的主要区别是this.get('store').commit()什么?
DS.Store = Ember.Object.extend(DS._Mappable, {
...
scheduleSave: function(record) {
get(this, 'currentTransaction').add(record);
once(this, 'flushSavedRecords');
},
...
/**
This method delegates committing to the store's implicit
transaction.
Calling this method is essentially a request to persist
any changes …Run Code Online (Sandbox Code Playgroud) 我已经扩展了此stackoverflow答案中给出的程序,以便有一个程序一次性删除所有记录.但是,删除仅在批处理中发生,并且不会一次删除所有内容.
这是我在这个JSBin中使用的函数的片段.
deleteAllOrg: function(){
this.get('store').findAll('org').then(function(record){
record.forEach(function(rec) {
console.log("Deleting ", rec.get('id'));
rec.deleteRecord();
rec.save();
});
record.save();
});
}
Run Code Online (Sandbox Code Playgroud)
知道如何修改程序,以便可以立即删除所有记录吗?
我也尝试过model.destroy()和model.invoke('deleteRecords')但它们不起作用.
任何帮助是极大的赞赏.谢谢你的帮助!