如何直接调用 Ember 数据函数? [英] How to call an Ember data function directly?

查看:14
本文介绍了如何直接调用 Ember 数据函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法调用 Ember 数据函数,例如序列化 函数,直接在我的索引页的主要 javascript 中?

Is there a way to call an Ember data function, e.g. the serialize function, directly inside my main javascript of the index page?

我尝试了 DS.Serializer.serialize(myrecord),但出现此错误:没有方法 'serialize'.

I tried DS.Serializer.serialize(myrecord), but I got this error: has no method 'serialize'.

补充问题:

如何直接调用RESTAdapter 的序列化版本?他们对 进行了一些修改通用序列化.

How can I directly call the RESTAdapter's version of serialize? They have made some modifications to the generic serialize.

推荐答案

有没有办法调用 Ember 数据函数,例如序列化函数,直接在我的索引页面的主 javascript 中?

Is there a way to call an Ember data function, e.g. the serialize function, directly inside my main javascript of the index page?

是和否.是的,因为 ember-data 函数没有什么特别之处(它们可以从任何地方调用)但是没有,因为调用 DS.Serializer.serialize(myrecord) 没有意义.

Yes and no. Yes in that there is nothing special about ember-data functions (they can be called from anywhere) but no because calling DS.Serializer.serialize(myrecord) does not make sense.

可能你想做的是序列化myrecord?在这种情况下尝试:

Probably what you are looking to do is serialize myrecord? In that case try:

myrecord.serialize()

余烬数据模型的 serialize() 函数将使用模型存储的序列化策略来返回模型的 JSON 表示.这意味着在 DS.Serializer 的实例上调用 serialize().请参阅 DS.Model.serialize()

The serialize() function of an ember-data model will use the serialization stragey of the model's store to return a JSON representation of the model. Under the hood that means calling serialize() on an instance of DS.Serializer. See DS.Model.serialize()

我尝试了 DS.Serializer.serialize(myrecord),但出现此错误:没有方法序列化"

I tried DS.Serializer.serialize(myrecord), but I got this error: has no method 'serialize'

没错.DS.Serializer 是一个类,没有 serialize 方法.所以 DS.Serializer.serialize(myrecord) 不起作用.您链接到的 serialize 函数是一个实例方法,因此它将可用于 DS.Serializer 类的实例.也就是说,DS.Serializer 是一个抽象基类,因此创建它的实例并尝试在其上调用 serialize() 没有多大意义.

Right. DS.Serializer is a class and has no serialize method. So DS.Serializer.serialize(myrecord) doesn't work. The serialize function you linked to is an instance method, so it will be available on instances of the DS.Serializer class. That said, DS.Serializer is an abstract base class so it would not make much sense to create an instance of it and try calling serialize() on that.

有关 ember-data 序列化程序如何工作的更多信息,请查看 [serializer api docs] (https://github.com/emberjs/data/blob/761412849a56ad086c44659faafa547d8f6c03a8/packages/ember-data/libjs/system/serializer.-L211)

More more info on how ember-data serializers work have a look at the [serializer api docs] (https://github.com/emberjs/data/blob/761412849a56ad086c44659faafa547d8f6c03a8/packages/ember-data/lib/system/serializer.js#L10-L211)

-- 更新附加问题:--

如何直接调用RESTAdapter的serialize版本?他们对通用序列化做了一些修改.

How can I directly call the RESTAdapter's version of serialize? They have made some modifications to the generic serialize.

如果使用 RESTAdapter,您只需在模型上调用 model.serialize().但是在极少数情况下,您的模型正在使用其他东西(例如 FixtureAdapter)并且您想要 RESTSerializer 的序列化功能,您可以使用 DS.RESTSerializer.create({}) 创建一个序列化器,然后通过您的模型到它的 serialize() 方法.例如:

If using RESTAdapter you could just call model.serialize() on a model. But in the rare case that your models are using something else (like FixtureAdapter) and you want serialization features of RESTSerializer, you can create a serializer using DS.RESTSerializer.create({}) and then pass your model to its serialize() method. For example:

App = Ember.Application.create();
App.Store = DS.Store.extend({adapter: 'DS.FixtureAdapter'});
App.Post = DS.Model.extend({
  title: DS.attr('string'),
  bodyText: DS.attr('string')
});
App.ApplicationRoute = Ember.Route.extend({
  model: function() {
    return App.Post.createRecord({title: 'This is my post', bodyText: 'There are many like it but this one is mine'});
  },
  afterModel: function(model, transition) {
    var serializer = DS.RESTSerializer.create({});
    console.log('FixtureSerializer via model.serialize(): ', model.serialize());
    console.log('RESTSerializer via serializer.serialize(post):', serializer.serialize(model));
  }
});

控制台日志输出为:

> FixtureSerializer via model.serialize():  Object {title: "This is my post", bodyText: "There are many like it but this one is mine"}
> RESTSerializer via serializer.serialize(post): Object {title: "This is my post", body_text: "There are many like it but this one is mine"}

您可以看到 RESTSerializer 将 bodyText 属性从驼峰式大小写转换为下划线.现场示例在这里:http://jsbin.com/uduzin/3/edit

You can see that RESTSerializer converts the bodyText attribute from camelCase to underscore. Live example is here: http://jsbin.com/uduzin/3/edit

这篇关于如何直接调用 Ember 数据函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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