Reputation: 1
I have a list on emberjs. I want to create a new elements of that list with data from server. The data are transmitted via ajax in json format and looks like this:
{
"status": "success",
"material":
[
{
"user1": "test",
"create_datetime": "2013-03-30 10:36:24+00:00"
}
]
}
How to fill ember.js
models of my list?
Upvotes: 0
Views: 430
Reputation: 1959
This can be done by iterating over your returned material data. (coffeescript)
Models
App.Material = Ember.Object.extend
userId: null
createDateTime: null
App.Item = Ember.Object.extend
status: null
materials: []
Controller
App.ItemController = Ember.ObjectController.extend
init: ->
@loadData()
loadData: =>
$.ajax
url: "/api/items"
success: (data) =>
if data?
# create an item object
item = App.Item.create
status: data.status
# loop through all materials for current item
for material in data.material
# push materials onto item.material stack
item.materials.pushObject App.Material.create
userId: material.user1
createDateTime: material.create_datetime
# set the controllers content to the item
@set('content') item
Upvotes: 1