Ember.js 基于余烬模型的积垢处理
在这里,我尝试使用实现CRUD操作。 我对灰烬环境完全陌生,实际上我对灰烬环境了解不多 在这里,我试图添加新产品并删除现有产品。我正在使用Ember.js 基于余烬模型的积垢处理,ember.js,ember-data,handlebars.js,ember-model,Ember.js,Ember Data,Handlebars.js,Ember Model,在这里,我尝试使用实现CRUD操作。 我对灰烬环境完全陌生,实际上我对灰烬环境了解不多 在这里,我试图添加新产品并删除现有产品。我正在使用fixture i、 e.购物车项目。My此装置包含两个节点,即登录和购物车项目,这就是我的装置结构: Astcart.Application.adapter = Ember.FixtureAdapter.create(); Astcart.Application.FIXTURES = [ { "logged_in": { "log
fixture
i、 e.购物车项目
。My此装置包含两个节点,即登录
和购物车项目
,这就是我的装置结构:
Astcart.Application.adapter = Ember.FixtureAdapter.create();
Astcart.Application.FIXTURES = [
{
"logged_in": {
"logged": true,
"username": "sachin",
"account_id": "4214"
},
"cart_items": [
{
"id": "1",
"name": "Samsung Galaxy Tab 2",
"qty": "1",
"price": "100",
"subtotal": "100"
},
{
"id": "2",
"name": "Samsung Galaxy Tab 2",
"qty": "1",
"price": "100",
"subtotal": "100"
},
{
"id": "3",
"name": "Samsung Galaxy Tab 2",
"qty": "1",
"price": "100",
"subtotal": "100"
}
]
}
];
我只想在服务器的一次服务调用中获取数据。
现在,这是我用来从cart\u项目添加和删除产品的代码
Astcart.IndexRoute = Ember.Route.extend({
model: function() {
return Astcart.Application.find();
}
});
Astcart.IndexController = Ember.ArrayController.extend({
save: function(){
this.get('model').map(function(application) {
var new_cart_item = application.get('cart_items').create({name: this.get('newProductDesc'),qty: this.get('newProductQty'),price: this.get('newProductPrice'),subtotal: this.get('newProductSubtotal')});
new_cart_item.save();
});
},
deleteproduct: function(product){
if (window.confirm("Are you sure you want to delete this record?")) {
this.get('model').map(function(application) {
application.get('cart_items').deleteRecord(product);
});
}
}
});
但当我试图保存产品时,我遇到了一个例外
Uncaught TypeError: Object [object global] has no method 'get'
当我试图删除产品时,我得到了一个异常
Uncaught TypeError: Object [object Object] has no method 'deleteRecord'
在这里,我还想实现一个功能,即在每次保存时,我都需要检查该产品是否已经存在。
如果产品不存在,则仅保存新产品,否则更新现有产品。
但我不知道怎么做
有人能帮我让这个JSFIDLE工作吗
更新
在这里,我并没有得到任何异常,但记录也并没有得到删除。
我不明白这里发生了什么
有人能帮我让这个JSFIDLE工作吗?在您的保存方法中“this”上下文会发生变化。您需要使用控制器的“this”,而不是map函数。试试这个:
save: function(){
var self = this;
self.get('model').map(function(application) {
var new_cart_item = application.get('cart_items').create({
name: self.get('newProductDesc'),
qty: self.get('newProductQty'),
price: self.get('newProductPrice'),
subtotal: self.get('newProductSubtotal')
});
new_cart_item.save();
});
}
不,仍然存在上下文问题。我得到了一个错误:“未捕获错误:Ember.Adapter必须实现createRecord”您的结构是小wierd。我想试试这样的东西<代码>Astcart.Application.find().get('cart\u items').get(product.get('id')).deleteRecord()
。但你的申请只是赤裸裸的。你能试着先从简单的模型开始,然后慢慢过渡到复杂的模型吗?还有为什么是Ember RC7?我的基本要求是在fixture中需要多个节点和数组。这样我就可以从服务器一次调用中获取数据。如果我的模型结构有任何问题,请向我解释“如何构建这样的模型结构?”我是emberjs新手,我将升级我的emberjs文件。如果您用我的JSFIDLE解释这个问题,将会更有帮助。