Thursday, 15 August 2013

EmberJS: clear data store and repopulate it

EmberJS: clear data store and repopulate it

Here's the deal: I'm trying to get the data from a php script, create
javascript objects and assign them to ember data store, then call ember
find() function to display the data.
Data Store:
App.Images = DS.Model.extend({
name: DS.attr('string'),
imageThumb: DS.attr('string'),
imageFull: DS.attr('string'),
});
//Have to do this, otherwise it doesn't work
App.Images.FIXTURES = [];
Here comes the main function, that loads the data... First of all, I'm not
even sure if this is the correct way. It does work, however, but... I'm
having serious doubts if it's supposed to be like that. Mind the comment
before calling the $.each function!
App.MainRoute = Ember.Route.extend({
model: function(){
$.ajax({
type: "POST",
url: "dataDao.php?f=getAll",
datatype: "json",
success: function(data){
//I WANT TO CALL THE 'CLEAR DATA STORE' HERE!
$.each(JSON.parse(data), function(index, value) {
App.Images.createRecord(
{
id: value['id'],
name: value['name'],
imageThumb: value['imageThumb'],
imageFull: value['imageFull'],
});
});
},
error: function(err){
alert('error!' + JSON.stringify(err));
}
});
return App.Images.find();
}
});
So, firstly, I'm not even sure if this is how ember is suppose to be used.
Secondly, isn't ajax suppose to be asynchronous? How come it waits for the
success function to finish before calling return App.Images.find()?
Shouldn't the return be called before the success function even finishes
and the display should be empty?
Thirdly (and finally): how do I clear the data store before creating new
objects? My results duplicate or rather, I keep adding same stuff to the
existing data store otherwise.
Thank you for your help!

No comments:

Post a Comment