你如何创建ember.js自定义适配器? [英] How do you create a custom adapter for ember.js?

查看:229
本文介绍了你如何创建ember.js自定义适配器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算用ember.js,但是我的REST API并不完全对准包装REST适配器。我想覆盖发现,并能够把我自己的AJAX在里面。我不喜欢怎么余烬的findAll检索所有我的文档没有选择分页,以便与其他查询参数将是有益的,可呈现就是为什么我想要写我自己的AJAX。我一直无法找到我怎么会去这样做的任何文件。

解决方案

有关Ember公司数据

这是为最新的灰烬数据1.0测试版9。

扩展灰烬数据适配器之一。为了使网站广泛:

  App.ApplicationAdapter = DS.RESTAdapter.extend(....
 

要使它具体产品型号:

  App.FooAdapter = DS.RESTAdapter.extend(...
 

然后你将定义你想覆盖的实施。你总是要叫 this._super 并恢复到基实现的选项。例如,

  App.NotesAdapter = DS.RESTAdapter.extend({
  发现:功能(存储,类型,识别){
    ID =富+ ID;
    返回this._super(专卖店,类型,识别);
  }
});
 

或者你可以完全覆盖的实现:

  App.NotesAdapter = DS.RESTAdapter.extend({
  发现:功能(存储,类型,识别){
    //做你的事在这里
    返回this.ajax(this.buildURL(type.typeKey,ID),GET);
  },

  的findAll:功能(存储,类型,sinceToken){
    //做你的事在这里
    VAR查询;

    如果(sinceToken){
      查询= {日期:sinceToken};
    }

    返回this.ajax(this.buildURL(type.typeKey),GET,{数据:查询});
  },

  findQuery:功能(存储,输入,查询){
    //做你的事在这里
    返回this.ajax(this.buildURL(type.typeKey),GET,{数据:查询});
  },

  findMany:功能(存储,类型,IDS,所有者){
    返回this.ajax(this.buildURL(type.typeKey),GET,{数据:{IDS:IDS}});
  },
   .....
});
 

要查看完整的API,您可以覆盖请参见:<一href="http://emberjs.com/api/data/classes/DS.RESTAdapter.html">http://emberjs.com/api/data/classes/DS.RESTAdapter.html

串行

通常更重要的将推出自己的序列化的按摩数据,以满足您的休息端点。下面是从过渡文档<一一些有用的信息href="https://github.com/emberjs/data/blob/master/TRANSITION.md">https://github.com/emberjs/data/blob/master/TRANSITION.md

短的版本是,一旦Ajax请求已经完成,将得到的有效负载是通过以下钩发送

  1. 在有效载荷发送到extractSingle,如果原来的请求是针对单条记录(如查找/保存)或extractArray,如果原来的请求是对一组记录(如的findAll / findQuery)
  2. 的这些方法的缺省行为是有效负载的顶层拉开成多个较小的记录。
  3. 在每个那些规模较小的记录发送到正常化,它可以在同一时间做归一化的记录。
  4. 最后,特定类型的记录可以专门归。

    App.PostSerializer = DS.RESTSerializer.extend({
      extractSingle:功能(存储,类型,有效载荷,ID){
        // 按摩
        this._super(专卖店,类型,有效载荷,ID);
      },
      extractArray:功能(存储,类型,有效载荷){
        // 按摩
        this._super(专卖店,类型,有效载荷);
      },
      标准化:函数(类型,哈希,财产){
        // 按摩
        this._super(类型,哈希,财产);
      }
    });

  • 使用extractSingle和extractArray当你的有效载荷的顶级组织不同于灰烬数据预期
  • 使用规格化正常化子哈希如果所有子哈希在有效载荷可以以相同的方式进行标准化。
  • 使用normalizeHash正常化的具体子哈希值。
  • 确保调用超级如果重写extractSingle,extractArray或正常化所以这个产业链的其余部分将被调用。

滚动自己

  App.FooAdapter = Ember.Object.extend({
  发现:功能(ID){
    返回$ .getJSON('http://www.foolandia.com/foooo/'+ ID);
  }
});
 

然后从你的路线,或其它地方

  App.FooRoute = Ember.Route.extend({
  型号:函数(){
    变种适配器= App.FooAdapter.create();
    返回adapter.find(1);
  }
});
 

现在我个人倒适配器注入到航线只是为了让我的生活更轻松:

  App.initializer({
    名称:fooAdapter

    初始化:功能(容器,应用程序){
        application.register(我的:经理人,application.FooAdapter);
        application.inject(控制器,fooAdapter,我的:经理人);
        application.inject(路线,fooAdapter,我的:经理人);
    }
});
 

然后沿途你可以懒散,做:

  App.FooRoute = Ember.Route.extend({
  型号:函数(){
    返回this.fooAdapter.find(1);
  }
});
 

例: http://emberjs.jsbin.com/OxIDiVU/676/edit

您可以阅读更多关于无烬灰烬数据:灰烬无烬数据

I'm planning on using ember.js, however my REST api doesn't exactly align with the packaged REST Adapter. I would like to "override" find and be able to put my own ajax in it. I dislike how an ember findAll retrieves all my documents with no options for pagination, so that along with other query parameters would be useful --which is why I want to write my own ajax. I've been unable to find any documentation on how I would go about doing this.

解决方案

For Ember Data

This is up to date as of Ember Data 1.0 beta 9.

Extend one of the Ember Data Adapters. To make it site wide:

App.ApplicationAdapter = DS.RESTAdapter.extend(....

To make it model specific:

App.FooAdapter = DS.RESTAdapter.extend(...

Then you will define the implementation you'd like to override. You always have the option to call this._super and revert to the base implementation. e.g.

App.NotesAdapter = DS.RESTAdapter.extend({
  find: function(store, type, id) {
    id = "foo" + id;
    return this._super(store, type, id);
  }
});

Or you can completely override the implementation:

App.NotesAdapter = DS.RESTAdapter.extend({
  find: function(store, type, id) {
    // Do your thing here
    return this.ajax(this.buildURL(type.typeKey, id), 'GET');
  },

  findAll: function(store, type, sinceToken) {
    // Do your thing here
    var query;

    if (sinceToken) {
      query = { since: sinceToken };
    }

    return this.ajax(this.buildURL(type.typeKey), 'GET', { data: query });
  },

  findQuery: function(store, type, query) {
    // Do your thing here
    return this.ajax(this.buildURL(type.typeKey), 'GET', { data: query });
  },

  findMany: function(store, type, ids, owner) {
    return this.ajax(this.buildURL(type.typeKey), 'GET', { data: { ids: ids } });
  },
   .....
});

To see the complete api you can override see: http://emberjs.com/api/data/classes/DS.RESTAdapter.html

Serializer

Often more important will be rolling your own serializer for massaging the data to fit your rest endpoint. Here's some useful information from the transition document https://github.com/emberjs/data/blob/master/TRANSITION.md .

The short version is that once an Ajax request has completed, the resulting payload is sent through the following hooks:

  1. The payload is sent to extractSingle if the original request was for a single record (like find/save) or extractArray if the original request was for an Array of records (like findAll/findQuery)
  2. The default behavior of those methods is to pull apart the top-level of the payload into multiple smaller records.
  3. Each of those smaller records is sent to normalize, which can do normalization a record at a time.
  4. Finally, specific types of records can be specially normalized.

    App.PostSerializer = DS.RESTSerializer.extend({
      extractSingle: function(store, type, payload, id) {
        // massage
        this._super(store, type, payload, id);
      },
      extractArray: function(store, type, payload) {
        // massage
        this._super(store, type, payload);
      },
      normalize: function(type, hash, property) {
        // massage
        this._super(type, hash, property);
      }
    });

  • use extractSingle and extractArray when the top-level of your payload is organized differently than Ember Data expects
  • use normalize to normalize sub-hashes if all sub-hashes in the payload can be normalized in the same way.
  • use normalizeHash to normalize specific sub-hashes.
  • make sure to call super if you override extractSingle, extractArray or normalize so the rest of the chain will get called.

Rolling your own

App.FooAdapter = Ember.Object.extend({
  find: function(id){
    return $.getJSON('http://www.foolandia.com/foooo/' + id);
  }
});

Then from your route, or wherever

App.FooRoute = Ember.Route.extend({
  model: function(){
    var adapter = App.FooAdapter.create();
    return adapter.find(1);
  }
});

Now personally I'd inject the adapter onto the routes just to make my life easier:

App.initializer({
    name: "fooAdapter",

    initialize: function (container, application) {
        application.register("my:manager", application.FooAdapter);
        application.inject("controller", "fooAdapter", "my:manager");
        application.inject("route", "fooAdapter", "my:manager");
    }
});

Then on the route you could be lazier and do:

App.FooRoute = Ember.Route.extend({
  model: function(){
    return this.fooAdapter.find(1);
  }
});

Example: http://emberjs.jsbin.com/OxIDiVU/676/edit

You can read more about Ember without Ember Data: Ember without Ember Data

这篇关于你如何创建ember.js自定义适配器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆