我已经阅读了一段时间的文档,但无法理解。我有以下代码:
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")
我试图获得每个用户点数的平均值,但每当我更新App.User时,它都不会再次运行AverageScore函数。我希望每次更新用户数组中的项目时都会运行它。但是,每次从数据集中删除用户时,它都会更新。如何在每次更改时更新?