2015-07-16 12 views
11

Dopo l'aggiornamento a 1,13 ottengo questa eccezione e non riesco a capire qual è il problema. Inoltre non sono riuscito a trovare alcuna risorsa utile che affronti il ​​mio problema.Hai modificato *** due volte in un unico rendering

Succede per le proprietà che ho impostato in un'altra proprietà calcolata. Ma questa proprietà è sicuramente chiamata solo una volta.

ho creato un esempio jsbin: http://emberjs.jsbin.com/roderameya/edit?html,js,console,output

UPDATE

come richiesto vi posto un codice che è più vicino alla mia implementazione reale.

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'), 

}); 

risposta

7

Cercare di avere le proprietà non impostate le altre proprietà, ma piuttosto dipendono l'uno dall'altro:

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

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

Updated jsbin for you.

UPDATE

In sostanza, il vostro resultCount è una variabile temporanea che siamo in grado di sbarazzarsi di, e il resto sono solo il concatenamento proprietà calcolate insieme:

updated jsbin for advanced example

codice:

// 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); 
    } 
    } 

}); 

Uso del modello:

<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 - ma sfortunatamente non è così facile. È un esempio molto semplificato che ho creato. Nella mia app c'è molta più logica. Quindi nella proprietà "someData" devo filtrare il modello e contare quanti oggetti filtrati ho. E non è solo un conteggio ma più valori. – val

+0

Lo stesso principio vale. Evita "set" all'interno di una proprietà calcolata. Prova ad estrarre ciascun conteggio in una proprietà calcolata. Se vuoi pubblicare un esempio più coinvolgente che è più vicino al tuo caso reale, possiamo vedere cosa possiamo fare. –

+0

Ok, ho appena aggiornato il post originale con un esempio. Questo è più o meno quello che ho. – val