2015-07-16 13 views
11

Sau khi nâng cấp lên 1.13 tôi nhận được ngoại lệ này và tôi không thể tìm ra vấn đề là gì. Tôi cũng không thể tìm thấy bất kỳ tài nguyên hữu ích nào giải quyết được vấn đề của mình.Bạn đã sửa đổi *** hai lần trong một lần hiển thị

Điều này xảy ra đối với các thuộc tính tôi đặt trong thuộc tính được tính toán khác. Nhưng tài sản này chắc chắn được gọi chỉ một lần.

Tôi đã tạo ra một ví dụ jsbin: http://emberjs.jsbin.com/roderameya/edit?html,js,console,output

CẬP NHẬT

Theo yêu cầu tôi đăng một số mã mà gần hơn để thực hiện thật của tôi.

Ember.Controller.extend({ 

    filter: '', 

    resultCount: { 
    total: 0, 
    matches: 0, 
    mismatches: 0 
    }, 

    results: function() { 
    var items = this.get('model'), 
     matches = [], 
     resultCount = {}; 

    // Apply search filter 
    matches = items.filter(function(item){ 
     // Just a dummy filter function 
     return true; 
    }); 

    // We need the total number matched by the filter string 
    resultCount.total = matches.length; 

    // The already matched items must be narrowed further down 
    matches = matches.filter(function(item) { 
     // Another filter function 
     return true; 
    }); 

    resultCount.matches = matches.length; 
    resultCount.mismatches = resultCount.total - matches.length; 

    this.set('resultCount', resultCount); 

    return matches; 

    }.property('model', 'filter'), 

}); 

Trả lời

7

Cố gắng có các thuộc tính của bạn không được thiết lập các tài sản khác, nhưng thay vì phụ thuộc vào nhau:

App.IndexController = Ember.Controller.extend({ 
    count: function() { 
    return this.get("data.length") || 0; 
    }.property('data.length'), 

    data: [1,2,3] 
}); 

Updated jsbin for you.

CẬP NHẬT

Về cơ bản, RESULTCOUNT của bạn là một biến tạm thời mà chúng ta có thể thoát khỏi, và phần còn lại chỉ là chaining tính tính với nhau:

updated jsbin for advanced example

mã:

// Index controller 
App.IndexController = Ember.Controller.extend({ 
    count: Ember.computed('filteredItems.length', function(){ 
    return this.get('filteredItems.length'); 
    }), 

    data: [ 
    Ember.Object.create({ name: "jim", age: 15 }), 
    Ember.Object.create({ name: "jeff", age: 42 }), 
    Ember.Object.create({ name: "eric", age: 7 }) 
    ], 

    filter: RegExp(".*"), 
    ageFilter: -1, 

    mismatchCount: Ember.computed('filteredItems.length', 'secondPassFilteredItems.length', function() { 
    return this.get('filteredItems.length') - this.get('secondPassFilteredItems.length'); 
    }), 

    filteredItems: Ember.computed('data', 'filter', function() { 
    var controller = this; 
    return this.get('data').filter(function(item) { 
     return item.get('name').match(controller.get("filter")); 
    }); 
    }), 

    secondPassFilteredItems: Ember.computed('filteredItems', 'ageFilter', function() { 
    var controller = this; 
    var ageFilter = controller.get("ageFilter"); 
    if (Ember.isEqual(ageFilter, -1)) { 
     return this.get('filteredItems'); 
    } else { 

     return this.get('filteredItems').filter(function(item) { 
     // more filtering 
     return item.get("age") <= ageFilter; 
     }); 
    } 
    }), 

    results: Ember.computed.alias('secondPassFilteredItems'), 

    actions: { 
    filterByJ: function() { 
     this.set('filter', new RegExp("j")); 
    }, 
    filterByEric: function() { 
     this.set('filter', new RegExp("eric")); 
    }, 
    filterAllNames: function() { 
     this.set('filter', new RegExp(".*")); 
    }, 
    filterYoungins: function() { 
     this.set('ageFilter', 15); 
    }, 
    filterAllAges: function() { 
     this.set('ageFilter', -1); 
    } 
    } 

}); 

Sử dụng mẫu:

<script type="text/x-handlebars" data-template-name="index"> 
    <p>Results found: {{count}}</p> 
    <p>Diff between first and second filter: {{mismatchCount}}</p> 
    <p>First Filter: 
     <button {{action 'filterAllNames'}}>all people</button> 
     <button {{action 'filterByJ'}}>People with J in name</button> 
     <button {{action 'filterByEric'}}>People named 'eric'</button> 
    </p> 
    <p> Second Filter: 
     <button {{action 'filterAllAges'}}>all ages</button> 
     <button {{action 'filterYoungins'}}>15 years old or younger</button> 
    </p> 
    <ul> 
    {{#each results as |item|}} 
     <li>{{item.name}} is {{item.age}} years old</li> 
    {{/each}} 
    </ul> 
    </script> 
+0

Thx - nhưng tiếc là không dễ dàng như vậy. Đó là một ví dụ rất đơn giản mà tôi đã tạo ra. Trong ứng dụng của tôi có nhiều logic hơn. Vì vậy, trong thuộc tính "someData", tôi phải lọc mô hình và phải đếm số lượng mục được lọc mà tôi có. Và nó không chỉ là một số mà còn nhiều giá trị. – val

+0

Giữ nguyên tắc tương tự. Tránh 'đặt' bên trong một thuộc tính được tính toán. Cố gắng trích xuất từng số vào một thuộc tính được tính toán. Nếu bạn muốn đăng một ví dụ liên quan nhiều hơn gần với trường hợp thực của bạn, chúng ta có thể thấy những gì chúng ta có thể làm. –

+0

Ok, tôi vừa cập nhật bài đăng gốc với một ví dụ. Đây là nhiều hơn hoặc ít hơn những gì tôi có. – val

Các vấn đề liên quan