dimanche 22 février 2015

How to tell Ember.js that Array has been updated

I have been reading the documentation for a while now and cannot figure it out. I have the following code:



App.User = DS.Model.extend({
name: DS.attr("string"),
points: DS.attr("float"),
isPassing: Ember.computed.gt("points", 65),
isEditingPoints: DS.attr("boolean", {defaultValue: false}),
isEditingName: DS.attr("boolean", {defaultValue: false})
});

App.User.FIXTURES = [
{
id: 1,
name: "Joe",
points: 85
},
{
id: 2,
name: "Cindy",
points: 90
},
{
id: 3,
name: "Ben",
points: 62
}
];

App.UserController = Ember.ObjectController.extend({
actions: {
editUserName: function() {
this.set("isEditingName", true);
},

editUserPoints: function() {
this.set("isEditingPoints", true);
},

acceptPointChanges: function() {
this.set("isEditingPoints", false);

if(Ember.isEmpty(this.get("model.points"))) {
this.send("removeUser");
}
},

acceptNameChanges: function() {
this.set("isEditingName", false);

if(Ember.isEmpty(this.get("model.name"))) {
this.send("removeUser");
}
},

removeUser: function() {
var user = this.get("model");
student.deleteRecord();
}
}
});

App.EditUserView = Ember.TextField.extend({
didInsertElement: function() {
this.$().focus();
}
});

Ember.Handlebars.helper("edit-user", App.EditUserView);



App.ApplicationController = Ember.ArrayController.extend({
actions: {
createStudent: function() {
var newName = this.get("newName");
var newPoint = this.get("newPoint")
var parsePoint = parseFloat(newPoint, 10);

if(!newName.trim() || isNaN(parsePoint)) {
return;
}

Count++;

var student = this.store.createRecord("student", {
id: Count,
name: newName,
points: parsePoint
});

this.set("newName", "");
this.set("newPoint", "");
}
},

AverageScore: function() {
var UserInfoArray = this.sortBy("id");

var sumOfPoints = 0;

console.log("ran AverageScore");

for(var i = 0; i < UserInfoArray.length; i++) {

var UserData = UserInfoArray[i]._data;
var UserAtt = UserInfoArray[i]._attributes;

if(isNaN(studentAtt.points)) {
sumOfPoints += studentData.points;
} else {
sumOfPoints += parseFloat(studentAtt.points,10);
}
}

if(sumOfPoints == 0) {
return "None";
}
return sumOfPoints/UserInfoArray.length;
}.property("@each")


I am trying to get the average of each user's points but whenever I update the App.User it does not run the AverageScore function again. I would like it to run every time I update an item in the array of users. However it only updates every time I remove a user from the dataset. How can I make it update on each change?





Aucun commentaire:

Enregistrer un commentaire