1
votes

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???

1
Is that all your code? Seems to be missing things? And like intuitivepixel mentioned below there are a few typos (like createRecord missing the closing braces...). Anyway, seems like you never defined the question variable that you are passing to your new record? And you are never adding that new record to the Userstats controller, so it would never find it when using findProperty on its model... and you might have to commit that new newStat too?colymba
Thank you for noticing! I left out important pieces. I must've overwritten them during an edit or something (it was late). I've updated code to reflect my real code and the behavior I experience - namely not being able to find a recently committed record with .findProperty on the Userstats controllerJoe B

1 Answers

1
votes

I don't know if it's a typo, but the computed property is defined the wrong way and should be like this:

App.QuestionController = Ember.ObjectController.extend({
  needs: 'userstats',
  choice: 'controllers.userstats.choice',
  chosen: function(key, value) {
    ...
  }.property('choice')
  ...
});

Inside the property() you should also define properties that trigger the computed property if they change. This way if choice changes the chosen cp will be triggered.

Please let me know if it helps.