Using Ember-Data 0.13-59 & Ember 1.0.0 RC 6 (from starter kit)
Problem: upon save()
to a new record made from App.Userstat.createRecord({ ... })
the server gets the POST
and successfully returns an id
but the new record is not available in the Userstat
model.
To better understand example: this is a quiz app(for multiple choice questions). Each question has several choices and when a user selects a choice, their choice to the corresponding question is stored in a Model, App.Userstat
.
At each question, the app needs to know whether the user has already answered this question or if it's new.
I use a computed property as a setter
and getter
. The setter
is called when a user selects a choice (the choice's value is passed to computed property). First it checks if a record exists for the user's current question. If it doesn't exist it will create a new record. If it does exist, it should only issue a PUT
request with updated content.
Code Updated(July 8, 11AM)
App.UserstatsController = Ember.ArrayController.extend();
App.QuestionController = Ember.ObjectController.extend({
needs: "userstats",
chosen = function(key, value) {
// getter
if(value === undefined) {
// something goes here
// setter
} else {
// the question.id is used to compare for an existing record in Userstat mdoel
var questionId = this.get('id');
var questionModel = this.get('model');
// does any Userstat record belong to the current question??
var stats = this.get('controllers.Userstats');
var stat = stats.get('model').findProperty('question.id', questionId);
// if no record exists for stat, means user has not answered this question yet...
if(!stat) {
newStat = App.Userstat.createRecord({
"question" : questionModel,
"choice" : value // value passed to the computed property
)}
newStat.save(); // I've tried this
// newStat.get('store').commit(); // and this
return value;
// if there is a record(stat) then we will just update the user's choice
} else {
stat.set('choice', value);
stat.get('store').commit();
return value;
}
}.property('controllers.Userstats')
No matter how many times I set chosen
it always sends a POST
(as opposed to an update only sending a PUT request) because it never adds the record to the model the first time.
To demonstrate further, in the setter
part of the computed property, when I put this code:
var stats = this.get('controllers.Userstats')
console.log stats
the Userstats controller shows all previously existing records, but not newly submitted records!
How come the new record isn't available after I save()
or commit()
it???
Thanks :)
EDIT
maybe it has something to do with me adding a record to the singular model App.Userstat
and then when I look for it, I'm searching using the UserstatsController which is an Array controller???
question
variable that you are passing to your new record? And you are never adding that new record to theUserstats
controller, so it would never find it when usingfindProperty
on its model... and you might have to commit that newnewStat
too? – colymba.findProperty
on theUserstats
controller – Joe B