EmberJS: creating a record that belongs.

Let me use posts and comments to describe my problem. Inside my post_controller, I want to create a new comment entry for this current post. What an ugly way to do this?

The ratio is established as follows:

App.Post = DS.Model.extend({
  comments: hasMany('comment'),
});

App.Comment = DS.Model.extend({
  post: belongsTo('post')
}); 

Inside my post_controller, I want to create a post. I have this inside an action that runs from a template:

App.PostController = Ember.ObjectController.extend({
  ...
  actions: {
    createComment: function() {
      var post = this.get('model'); // Edit: Forgot that I had this declared outside createRecord
      var comment = this.store.createRecord('comment', {
        content : "content",
        post : post // This is where the problem is
      });
    }
  }
});

However, I get an error: Uncaught TypeError: cannot read the 'post' property from undefined

How to declare this relationship? Thanks.

Edit: An ember data error arises from this internal function in ember-data.js:

return Ember.computed(function(key, value) {
    var data = get(this, 'data'),
        store = get(this, 'store'), belongsTo, typeClass;

    if (typeof type === 'string') {
      typeClass = store.modelFor(type);
    } else {
      typeClass = type;
    }

    if (arguments.length === 2) {
      Ember.assert("You can only add a '" + type + "' record to this relationship", !value || value instanceof typeClass);
      return value === undefined ? null : value;
    }

    belongsTo = data[key]; // ERROR OCCURS HERE! 

    if (isNone(belongsTo)) { return null; }

    store.fetchRecord(belongsTo);

    return belongsTo;
  }).property('data').meta(meta);
};

EDIT: The problem is solved!

, , . ember. .

+4
1

createRecord (.. var comment =)

var postModel = this.get('model');

"this" createRecord

this.get('model') this.get('content') (- )?

0

All Articles