计算子总计emberjs

编程入门 行业动态 更新时间:2024-10-27 06:20:26
本文介绍了计算子总计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

本文发布于:2023-10-30 22:20:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544169.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算子   emberjs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!