重新计算Ember计算的属性,而不更改其依赖项

时间:2014-08-28 05:30:10

标签: ember.js

我有一个计算属性(名为userFields),具体取决于另一个计算属性(名为userList)。当对象(它是一个视图)被初始化时,我找到了属性" userFields"使用" userList"计算了数百次。根据控制台日志,只计算一次。我不知道为什么会这样。

以下是我的代码:

 App.MainServiceInfoApplicationQueueUserGroupSelectView = Ember.View.extend({
     templateName: require('templates/main/service/info/applicationqueues/user_group_select'),

     userCollapse: false,
     groupCollapse: false,

     toggleUserCollapse: function() {
         myview = this;
         this.set('userCollapse', !this.get('userCollapse'));
     },
     toggleGroupCollapse: function() {
         this.set('groupCollapse', !this.get('groupCollapse'));
     },

     userList: function() {
         console.log('calc user list'); // This is printed only once
         var list = [];
         App.User.find().forEach(function(item) {
             list.push(item);
         });
         return list;
     }.property('App.User.find()'),

     groupList: function() {
         var list = [];
         App.Resourcegroup.find().forEach(function(item) {
             list.push(item);
         });
         return list;
     }.property('App.Resourcegroup.find()'),

     userFields: function() {
         console.log('calc user fields'); // This is printed hundreds of times
         // Parse value
         var parsedValues = [];
         try {
             var usersFromValue = this.get('value').split(' ')[0].split('/');
             for (var i = 0; i < usersFromValue.length; i++)
                 parsedValues.push(usersFromValue[i]);
         } catch (e) {}

         var fields = [];
         for (var i = 0; i < this.get('userList').length; i++) {
             var item = this.get('userList')[i];
             fields.push(Ember.Object.create({
                 viewClass: Ember.Checkbox.extend({
                     value: false,
                     checkedBinding: 'value'
                 }),
                 displayName: item.get('userName'),
                 name: item.get('userName'),
                 value: parsedValues.contains(item.get('userName'))
             }));
         }
         return fields;
     }.property('userList'),
     groupFields: function() {
         // Parse value
         var parsedValues = [];
         try {
             var groupsFromValue = this.get('value').split(' ')[1].split('/');
             for (var i = 0; i < groupsFromValue.length; i++)
                 parsedValues.push(groupsFromValue[i]);
         } catch (e) {}

         var fields = [];
         for (var i = 0; i < this.get('groupList').length; i++) {
             var item = this.get('groupList')[i];
             fields.push(Ember.Object.create({
                 viewClass: Ember.Checkbox.extend({
                     value: false,
                     checkedBinding: 'value'
                 }),
                 displayName: item.get('groupName'),
                 name: item.get('groupName'),
                 value: parsedValues.contains(item.get('groupName'))
             }));
         }
         return fields;
     }.property('groupList'),

     users: function() {
         console.log('calc users'); // This is printed as many times as 'calc user fields' was printed
         var list = [];
         for (var i = 0; i < this.get('userFields').length; i++) {
             var theField = this.get('userFields')[i];
             if (parseBoolean(theField.get('value')) == true) list.push(theField.get('name'));
         }
         return list;
     }.property('userFields, userFields.@each.value'),
     groups: function() {
         var list = [];
         for (var i = 0; i < this.get('groupFields').length; i++) {
             var theField = this.get('groupFields')[i];
             if (parseBoolean(theField.get('value')) == true) list.push(theField.get('name'));
         }
         return list;
     }.property('groupFields, groupFields.@each.value'),

     usersString: function() {
         return this.get('users').join(', ');
     }.property('users'),
     groupsString: function() {
         return this.get('groups').join(', ');
     }.property('groups'),

     usersValue: function() {
         return this.get('users').join('/');
     }.property('users'),
     groupsValue: function() {
         return this.get('groups').join('/');
     }.property('groups'),

     value: function() {
         return this.get('usersValue') + " " + this.get('groupsValue');
     }.property('usersValue', 'groupsValue')
 });

0 个答案:

没有答案