Dor*_*don 13 javascript model-view-controller ember.js ember-data
Ember数据仍未达到版本1.0,因此我决定使用Ember而不使用Data模型.
我有自己的模型,这些模型由路径模型函数创建.
但是,保持前端对象和后端对象之间的状态是一场噩梦.特别是当一条路线使用其他路线模型时.
我正在做最好的做法,还是我应该采取不同的做法?我的直觉是,如果不使用Ember Data,我应该写自己的商店.我很乐意收到你们中的一些人的反馈.
模型示例:
App.Person = Ember.Object.extend(App.Serializable,Em.Copyable,{
user_email : null //used in routing dynamic segements and as old email when making changes to email
,first_name: null
, last_name: null
, fullname : function () {
return this.first_name + ' ' + this.last_name;
}.property('first_name','last_name').cacheable()
};
App.Person.reopenClass({
createRecord: function(data) {
return App.Person.create({
user_email : data.email
, first_name: data.first_name
, last_name : data.last_name
}});
Run Code Online (Sandbox Code Playgroud)
我如何加载类模型的示例:
App.UsersRoute = App.AuthenticatedRoute.extend( {
model : function () {
return new Ember.RSVP.Promise(function(resolve, reject) {
$.getJSON('/users').then(function(usersData) {
var userObjects = [];
usersData.forEach(function (data) {
userObjects.pushObject(App.Person.createRecord(data));
});
resolve( userObjects);
},function(error) {
reject(error);
});
})
},
Run Code Online (Sandbox Code Playgroud)
子路由使用该模型:
App.UsersAvailableRoute = App.AuthenticatedRoute.extend( {
model : function () {
return {
allUsers : Ember.ArrayController.create({
content : this.modelFor('users').filter( function (user) {
return user.availablity === 100
}),
Run Code Online (Sandbox Code Playgroud)
我如何在控制器中更新模型的示例:
App.UsersAvailableController = Ember.ArrayController.extend({
needs : ['users']
,applyPersonAssign : function (person,need,project) {
need.set('allocated',person);
var updateObject = Ember.copy(project,true);
if (Ember.isEmpty(need.get('inProject'))) {
updateObject.projectNeeds.pushObject(need);
}
return $.ajax({
url: '/projects/' + updateObject.get('codename'),
"type": "PUT",
"dataType": "json",
data: updateObject.serialize()
})
Run Code Online (Sandbox Code Playgroud)
Kin*_*n2k 22
您不一定需要重新创建Ember数据存储.Ember可以与POJO一起使用,您也可以将您的POJO包装在Ember对象中,为您提供一些有趣的内置功能.
据说创建一个缓存结果的自定义适配器可能很方便.
这是我创建支持缓存的适配器的示例.您可以慢慢地构建所需的所有基本功能的概念.
App.FooAdapter = Ember.Object.extend({
cache:{},
find: function(id){
var self = this,
record;
if(record = this.cache[id]){
return Ember.RSVP.cast(record);
}
else
{
return new Ember.RSVP.Promise(function(resolve){
resolve($.getJSON('http://www.foolandia.com/foooo/' + id));
}).then(function(result){
record = self.cache[id] = App.Foo.create(result);
return record;
});
}
}
});
Run Code Online (Sandbox Code Playgroud)
在下面的例子中,我使用容器在我的所有路由/控制器上注册适配器,所以我懒得轻松访问它.
http://emberjs.jsbin.com/OxIDiVU/742/edit
如果你总是希望它成为一个承诺:
http://emberjs.jsbin.com/OxIDiVU/740/edit
上面的例子可能会让你看起来像你需要做大量的工作,但不要忘记,Ember是超级可重用的,利用魔法.
App.MyRestAdapter = Em.Object.extend({
type: undefined,
find: function(id){
$.getJSON('http://www.foolandia.com/' + this.get('type') + '/' + id
}
});
App.FooAdapter = App.MyRestAdapter.extend({
type: 'foo' // this would create calls to: http://www.foolandia.com/foo/1
});
App.BarAdapter = App.MyRestAdapter.extend({
type: 'bar' // this would create calls to: http://www.foolandia.com/bar/1
});
Run Code Online (Sandbox Code Playgroud)
这是Ember Data/Ember Model的基本思想.他们试图创造大量的默认值并建立在酷炫之中,但有时它太过分了,特别是如果你只是消耗数据而不是做CRUD.
示例:http://emberjs.jsbin.com/OxIDiVU/744/edit
你也可以读这个(说同样的东西):