Examples of creating models without using Ember Data in Ember.js

Source: Internet
Author: User
Tags extend

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.