ember.js - Manipulating data from AJAX call in Ember js -
i'm new ember js , i'm having difficulty seeing why isn't working. i'm sending request server , giving me array. take array display contents.
app.js
app.testroute = ember.route.extend({ model: function(){ return app.test.findall(); } }); app.test = ember.object.extend(); app.test.reopenclass({ findall: function() { var dummyarray = []; $.ajax({ type: 'get', url: 'myurl', headers: {'myheader'}, success: function(data){ data.dummyarray.foreach(function (item){ dummyarray.push(app.test.create(item)); }); return dummyarray; }, error: function(request, textstatus, errorthrown){ alert(errorthrown); console.log(); } }); } });
when test page action should fire , array should returned model data can grabbed populate page
and in html have this:
script type="text/x-handlebars" id="test"> <ul> {{#each item in model}} <li>{{item.id}}</li> {{/each}} </ul> {{outlet}} </script>
in console when log data returned looks this:
object {dummyarray: array[4]} dummyarray: array[4] 0: object id: 1111 1: object id: 1112 2: object id: 1113 3: object id: 1114
the app runs no errors when navigate test page page not populate data.
your problem synchronous code returning nothing. model
function returns app.test.findall()
, never anything. need return promise.
findall: function () { var result = []; return new ember.rsvp.promise(function (resolve, reject) { ember.$.ajax({ type: 'get', url: 'myurl', headers: { 'myheader' }, success: function (data) { data.dummyarray.foreach(function (item) { result.push(app.test.create(item)); }); resolve(result); }, error: function (request, textstatus, error) { console.log(error); reject(error); } }); }); }
Comments
Post a Comment