Hello. I can not figure out how to push json into a specific object. I make a blog with categories, where in categories there is a field hasMany ().

enter image description here

I can not exactly get into the collection. Here is my sample add code.

var catId = this.get('category'); var postId = newPost.id; newPost.save(); var addtocat = this.store.push('category', catId.posts, { postId : true }); addtocat.save(); 

I add the post myself, then I take the id from the post and insert it into the category.

    1 answer 1

    Good day. The push method of the store class is not necessary. It is very rarely used. It is used when, for example, an adapter / serializer is written. And in general, if I'm not mistaken, the push method takes an argument in the form of an object in json-api format. You should have something like this:

     // app/models/post.js export default DS.Model.extend({ autor: DS.attr("string"), body: DS.attr("string"), publish: DS.attr("date"), title: DS.attr("string"), category: DS.belongsTo("category") }); // app/models/category.js export default DS.Model.extend({ name: DS.attr("string"), path: DS.attr("string"), posts: DS.hasMany("post") }) 

    And in any place (route) you write those. start working

     // создаете post например так let post = this.store.createRecord('post', { autor: "Vasya", body: "Body", publish: new Date(), title: "Title", }); // допустим сохраним на сервере post.save(); // затем создадим category let category = this.store.createRecord('category', { name: "Технологии", path: "Tech" }); category.get("posts").pushObject(post); category.save(); 

    Something like this. Somewhere could of course be wrong.

    Links: https://guides.emberjs.com/v2.4.0/models/relationships/ http://emberjs.com/api/data/classes/DS.Model.html#stq=&stp=0