Note: This article is based on the Ember.js 1.4.0 version
1. Don't like it
2. It's not stable.
3. Back-end development of the RESTFUL3 API style is not good enough, Ember Data does not apply
4. Other
Of course, not without it will not be what, because we have a lot of lightweight alternatives 4, if we do not like it, we can also hand-handwriting 5 ajax to create a ember.js model part of 6.
The code is as follows |
Copy Code |
Window. APP = Ember.Application.create (); Create an APP instance App.Router.map (function () { This.route (' person '); }); App.model = Ember.Object.extend ({}); Define a model class, all model inherits from it App.person = App.Model.extend ({ Firstname:null, Lastname:null, Age:null, Gender:null }); Define a person class Assume that the API interface for all person is http://www.111cn.net///returned data structure is {"People": [{"FirstName": "Sam", "LastName": "Chen", "Age": 18 , "gender": "Male"},{"FirstName": "Jane", "lastName": "White", "Age": "Gender": "Female"}]} |
Next, add a method to the person class with REOPENCLASS7, which is used to return all instances of person:
The code is as follows |
Copy Code |
App.Person.reopenClass ({ Findall:function () { Return $.get (' Http://www.111cn.net '). Then ( Function (response) { return Response.people.map (the function (person) {//Map is the method of JavaScript return App.Person.create (person); }); } ); } }); |
In this way, we can set the Model in the Route:
The code is as follows |
Copy Code |
App.personroute = Ember.Route.extend ({ Model:function () { return App.Person.findAll (); }, Setupcontroller:function (Controller, model) { Controller.set (' model ', model); Can also be written as Controller.set (' content ', model) } }); |
You can then use the model data in the Handlebars.js template 8:
code is as follows |
copy code |
<table Class=table> <thead> <TR> <th> name </th> <th> Age </th> <th> sex </th> </TR> </ Thead> <tbody> {{#each}} <tr> <td>{{firstname} {{ Lastname}}</td> <td>{{age}}</td> <td>{{ Gender}}</td> </tr> {{/each}} </tbody> </table> |