ember.js – 使用EmberData手动加载模型

栏目: 编程语言 · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/15907029/load-a-model-manually-with-emberdata
我有一个带有登录表单的Ember应用程序,它在成功登录后以 JSON

格式返回当前用户.

使用createRecord直接在模型上设置返回的JSON属性.例如,is_private变为user.is_private,而不是user.get(‘isPrivate’)?

如何加载用户模型以便正确设置属性并且我不必使用id重新获取它?

据说,官方的方法是使用adapter.load,如 described in this thread

Loading Data

Previously, some features of the store, such as load() , assumed a

single adapter.

If you want to load data from your backend without the application

asking for it (for example, through a WebSockets stream), use this

API:

store.adapterForType(App.Person).load(store, App.Person, payload);

This API will also handle sideloaded and embedded data. We plan to add

a more convenient version of this API in the future.

但不幸的是,尽管文档声称,它并不处理侧载数据.我个人使用类似下面的内容,它基于如何实现find(ID):

var id = json["person"]["id"];
var store = DS.get("defaultStore");
var adapter = store.adapterForType(App.Person);
adapter.didFindRecord(store, App.Person, json, id);
var person = App.Person.find(id);

请注意,此代码假定JSON的格式与find(ID)期望从服务器接收的格式相同,如 documented in the RESTAdapter guide

{
  person: {
    id: 1,
    is_private: false,
    projects: [3]
  },
  projects: [
    { id: 3, name: "FooReader" }
  ]
}

这将应用您使用keyForAttributeName配置的任何转换(例如将is_private映射到isPrivate),它将处理侧载记录.我不确定这是不是最好的做法,但效果很好.

翻译自:https://stackoverflow.com/questions/15907029/load-a-model-manually-with-emberdata


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

从零开始做产品经理

从零开始做产品经理

萧七公子 / 中国华侨出版社 / 2016-12-1 / 27.9

《从零开始做产品经理:产品经理的第一本书》根据产品经理的能力需求与成长体系,共分为八章内容,从了解产品开始,到挖掘用户需求、进行产品设计、管理团队、进行项目管理、产品运营、把握产品的生命周期,以及产品经理的成长路径,全面阐释了产品经理的修炼之道。《从零开始做产品经理:产品经理的第一本书》书中信息量大,图文并茂,论点与论据相得益彰,并且融合了丰富的案例与故事,使得整个阅读过程妙趣横生而且迅速开“悟道......一起来看看 《从零开始做产品经理》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具