灰烬,无灰烬数据

多莉·齐登(Dory Zidon)

Ember数据仍不是1.0版,因此我决定使用没有Data模型的Ember。

我有自己的模型,这些模型是由路径模型功能创建的。

但是,保持前端对象和后端对象之间的状态是一场噩梦。尤其是当一条路线使用另一条路线模型时。

  • 如何实现此目的,我应该编写自己的商店和模型查找方法吗?
  • 我应该使用Ember Data(即使不是1.0版)还是Ember Data 1.0上的ETA?
  • 每次更改模型时,编写代码以更新前端的模型吗?
  • 另一种方法?

我是在做最佳实践,还是应该以不同的方式做?我的直觉是,在不使用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
}});

我如何加载类模型的示例:

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);
      });
    })
  },

子路由使用以下模型:

App.UsersAvailableRoute = App.AuthenticatedRoute.extend( {
     model : function () {
        return {
          allUsers :  Ember.ArrayController.create({
            content : this.modelFor('users').filter( function (user) {
                      return user.availablity === 100
                      }),

我如何在控制器中更新模型的示例:

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()
          })
劲派2k

您不一定需要重新创建Ember Data存储。Ember与POJO配合得很好,您也可以将POJO封装在Ember Object中,从而为您提供一些有趣的内置功能。

话虽如此,创建一个缓存结果的自定义适配器可能很方便。

这是一个示例,其中我创建了一个支持缓存的适配器。您可以根据需要的所有基本内容慢慢构建该概念。

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;
      });
    }
  }
});

在下面的示例中,我使用容器在所有路由/控制器上注册适配器,因此我可以轻松懒惰地访问它。

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
});

这是什么是Ember Data / Ember Model的基本思想。他们试图创建大量默认值并内置一些功能,但是有时候这样做有些过分,特别是如果您只是在使用数据而不进行CRUD的话。

示例:http//emberjs.jsbin.com/OxIDiVU/744/edit

您也可以阅读以下内容(说同样的话):

如何为ember.js创建自定义适配器?

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章