我正在尝试将对象添加到我的 Emberjs Arraycontroller。我有一个“创建”动作,当按下按钮时会触发。这工作正常,但我似乎无法将带有 this.pushObject 函数的元素添加到 ArrayController。我收到此错误消息:
Uncaught Error: The result of a server query (on App.Software) is immutable.
我想这是因为我正在使用 RESTAdapter 加载数据并且不喜欢我手动添加元素?
这是我的控制器和创建操作。
App.SoftwareIndexController = Ember.ArrayController.extend({
sortProperties: ['revision'],
create:function(){
var revision = $('#software_revision').val();
var doc = $('#software_document').val();
var software = App.Software.createRecord({
product_id: 1,
revision: revision,
doc: doc
});
this.pushObject(software);
}
});
这是路线
App.SoftwareIndexRoute = Ember.Route.extend({
setupController:function(controller){
var product_id = 1;
controller.set('content', App.Software.find({product_id:1}));
}
});
这是模型和商店
App.Store = DS.Store.extend({
revision: 12,
adapter: 'DS.RESTAdapter'
});
DS.RESTAdapter.configure("plurals", {
software: "software"
});
App.Software = DS.Model.extend({
revision: DS.attr('string'),
doc: DS.attr('string'),
verified: DS.attr('boolean')
});
这是带有创建表单和软件列表的模板视图
<script type="text/x-handlebars" data-template-name="software/index">
<p>
<fieldset>
<legend>Create a new software revision</legend>
<label for="software_revision">Revision</label>
<input id="software_revision" name="software_revision" type="text" placeholder="">
<label for="software_document">Document ID</label>
<input id="software_document" name="software_document" type="text" placeholder="">
<button class="btn btn-success" {{action create}}>Create</button>
</fieldset>
</p>
{{#if length}}
<table class="table">
<thead>
<tr>
<th>Revision</th>
<th>Created</th>
</tr>
</thead>
<tbody>
{{#each controller}}
<tr>
<td>{{revision}}</td>
<td>{{createdAt}}</td>
</tr>
{{/each}}
</tbody>
</table>
{{else}}
<div class="alert alert-info">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>No software revisions found!</strong> start by creating a new revision above.
</div>
{{/if}}
</script>
有人知道将新对象添加到 ArrayController 存储的正确方法吗?谢谢!
顺便说一句,如果我更改路线,这样它就不会使用 RESTAdapter
App.SoftwareIndexRoute = Ember.Route.extend({
setupController:function(controller){
var product_id = 1;
controller.set('content', []); // not using the RESTAdapter to load data
}
});