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

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

问题描述

有没有办法调用Ember数据功能,例如。 serialize 功能,直接在我的主页的索引页面?



我尝试过 DS.Serializer .serialize(myrecord),但是我收到这个错误:没有方法'serialize'






其他问题:



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

解决方案


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


是和否。是的,没有什么特别的关于ember数据功能(他们可以从任何地方调用),但不是因为调用 DS.Serializer.serialize(myrecord)没有意义。



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

  myrecord.serialize()

ember数据模型的 serialize()函数将使用模型商店的序列化stragey返回一个JSON模型的表示。在引擎盖下,意味着在DS.Serializer的一个实例上调用serialize()。请参阅 DS。我试过DS.Serializer.serialize(myrecord),但是我收到了这个错误:没有方法'serialize'


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



有关ember数据序列化程序的工作原理的更多信息,请参阅[serializer api docs]( https://github.com/emberjs/data/blob/761412849a56ad086c44659faafa547d8f6c03a8/packages/ember -data / lib / system / serializer.js#L10-L211



- 更新为其他问题: -


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


如果使用RESTAdapter,您可以调用 model.serialize )在模型上。但是,在极少数情况下,您的模型正在使用别的东西(如FixtureAdapter),并且您希望使用RESTSerializer的序列化功能,您可以使用 DS.RESTSerializer.create({})然后将你的模型传递给它的 serialize()方法。例如:

  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很多都喜欢它,但这是我的'});
},
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通过model.serialize():Object {title:这是我的帖子,bodyText:有很多喜欢它,但这是我的} 
> RESTSerializer通过serializer.serialize(post):Object {title:这是我的帖子,body_text:有很多喜欢它,但这一个是我的}

您可以看到RESTSerializer将bodyText属性从camelCase转换为下划线。例如: http://jsbin.com/uduzin/3/edit


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

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


Additional question:

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

解决方案

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

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.

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

myrecord.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()

I tried DS.Serializer.serialize(myrecord), but I got this error: has no method '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.

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)

-- UPDATED for Additional question: --

How can I directly call the RESTAdapter's version of serialize? They have made some modifications to the generic 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));
  }
});

Console log output will be:

> 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"}

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天全站免登陆