本文介绍了计算子总计emberjs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个商店购物车。我使用夹具适配器。 我的模型
App.Clothing = DS.Model.extend({ name:DS.attr ('string'),category:DS.attr('string'),img:DS.attr('string'),price:DS.attr('number' ,num:DS.attr('number'),fullPrice:function(){ return this.get('price')+$; }。属性('price')}) App.CartRecord = App.Clothing.extend({ numInCart:DS.attr('number',{defaultValue:1} ),fullPrice:function(){ return this.get('price')* this.get('numInCart'); } .property('numInCart','price' )}) App.CartRecord.FIXTURES = [];路线
App.CartRoute = Em.Route.extend({ model:function(){ return this.store.find('cartRecord'); } })我的控制器
App.CartController = Em.ArrayController.extend({ totalPrice:0 });我如何计算总价?
sum 的reduceComputed属性放在一起。以下是几个灵感链接:一个, two 和,html#method_reduceComputed =nofollow>基本上你可以这样做: Emberputed.sum = function(dependentKey){ return Ember .reduceComputed.call(null,dependentKey,{ initialValue:0, addedItem:function(cumulativeValue,item,changeMeta,instanceMeta){ return accumulationValue + item; $, removedItem:function(cumulativeValue,item,changeMeta,instanceMeta){ return accumulationValue - item; } }); };然后,在您的控制器中执行如下操作:
App.CartController = Em.ArrayController.extend({ price:Emberputed.mapBy('content','fullPrice'), totalPrice :Emberputed.sum('price')});
I create a shop cart. I using fixture adapter. My models
App.Clothing = DS.Model.extend({ name: DS.attr('string') , category: DS.attr('string') , img: DS.attr('string') , price: DS.attr('number') , num: DS.attr('number') , fullPrice: function(){ return this.get('price') + " $"; }.property('price') }) App.CartRecord = App.Clothing.extend({ numInCart:DS.attr('number',{defaultValue:1}) , fullPrice: function(){ return this.get('price')*this.get('numInCart'); }.property('numInCart','price') }) App.CartRecord.FIXTURES = [];Route
App.CartRoute = Em.Route.extend({ model: function(){ return this.store.find('cartRecord'); } })And my controller
App.CartController = Em.ArrayController.extend({ totalPrice: 0 });How i can calculate a total price?
解决方案You can put together a reduceComputed property for sum. Here are a few links for inspiration: one, two, and three. Basically, you can do something like this:
Emberputed.sum = function (dependentKey) { return Ember.reduceComputed.call(null, dependentKey, { initialValue: 0, addedItem: function (accumulatedValue, item, changeMeta, instanceMeta) { return accumulatedValue + item; }, removedItem: function (accumulatedValue, item, changeMeta, instanceMeta) { return accumulatedValue - item; } }); };Then, in your controller do something like this:
App.CartController = Em.ArrayController.extend({ prices: Emberputed.mapBy('content', 'fullPrice'), totalPrice: Emberputed.sum('prices') });
更多推荐
计算子总计emberjs
发布评论