我正在使用active_model_serializers和ember.js.我的一个模型有一个日期属性.在rails中,日期属性以"YYYY-MM-DD"的格式序列化.
问题; 当ember-data使用javascript Date构造函数对日期进行反序列化时,它会假定"不正确"的时区.
*不正确不是最好的词,但它是不正确的,因为我希望它默认为当前时区.DS.Model日期属性错误地解析日期(YYYY-MM-DD)
我认为active_model_serializer应该采用date属性并将其转换为iso8601格式.
Object.date.to_time_in_current_zone.iso8601
Run Code Online (Sandbox Code Playgroud)
有没有办法告诉active_model_serializers如何序列化所有日期对象?或者我应该在javascript中修复时区问题?
如何在不删除子项的情况下从hasMany关系中删除子项?
我已经尝试将子项的foreign_key设置为null.我也尝试在父关系上使用removeObject.
这是一个例子.
App.Invoice = DS.Model.extend
lines: DS.hasMany('App.Line')
App.Line = DS.Model.extend
invoice: DS.belongsTo('App.Invoice')
App.InvoiceController = Ember.Controller.extend
removeLine: (line) ->
@get('content.lines').removeObject(line)
line.set('invoice', null)
@get('store').commit()
App.InvoiceEditView = Ember.View.extend
templateName: 'invoice'
App.LineView = Ember.View.extend
tagName: 'tr'
templateName: 'line'
#invoice template
<table>
{{#each content.tasks}}
{{view App.LineView}}
{{/each}}
</table>
#line template
<td><a {{action "removeLine" view.context}}>remove</a></td>
<td>{{description}}</td>
<td>{{price}}</td>
<td>{{price}}</td>
Run Code Online (Sandbox Code Playgroud)
我目前正在使用
jquery 1.8.2
ember.js v1.0.pre-4
ember-data v11
Run Code Online (Sandbox Code Playgroud) 有没有人对如何使用ember-data手动创建自联接关系有任何建议?
例如,如果用户有许多关注者(其他用户),那么将这种数据结构构建为ember-data的最简单方法是什么?
我想在创建帖子后进行转换.
post/new> click submit> rails backend成功创建帖子并响应json>重定向到新创建的帖子的路径
在ember_data_example github源代码中.他们使用这种方法
transitionAfterSave: function() {
// when creating new records, it's necessary to wait for the record to be assigned
// an id before we can transition to its route (which depends on its id)
if (this.get('content.id')) {
this.transitionToRoute('contact', this.get('content'));
}
}.observes('content.id'),
Run Code Online (Sandbox Code Playgroud)
它工作正常,因为模型在创建模型时ID为null,并且当模型保存成功时其ID将更改,因为此函数会观察模型ID的更改.
但也许,只要模型的ID属性发生变化,就会执行此功能.我发现了更多的语义方式.
我希望在模型的状态更改为'isDirty'= false &&'isNew'== true form''isDirty'= true,'isNew'= false时执行转换.
我该如何实现呢?
我有这个小Ember应用程序:
window.App = Ember.Application.create();
App.Store = DS.Store.extend({
revision: 11,
adapter: DS.FixtureAdapter({
simulateRemoteResponse: false
})
});
App.Model = DS.Model.extend({
title: DS.attr('string')
});
App.Model.FIXTURES = [];
App.ready = function() {
console.dir(App.Model.find().get('length'));
App.Model.createRecord({id: 1, title: "TEST"});
console.dir(App.Model.find().get('length'));
console.dir(App.Model.find(1).get('title'));
};
Run Code Online (Sandbox Code Playgroud)
我得到了正确的标题console.dir(App.Model.find(1).get('title')但两个get('length')电话都返回0.我错过了什么?
这是一个(非)工作的jsbin:http://jsbin.com/uxalap/6/edit
在我的路线中,我有一个尝试从服务器请求模型列表的方法
model: ->
App.MyModel.find
projectId: (@modelFor "project").id
Run Code Online (Sandbox Code Playgroud)
现在显然有时这可能会返回404.
在发生这种情况的那一刻,Ember停止做任何事情.没有渲染视图,没有设置控制器.
那么我该如何正确处理404(即显示错误视图)?
我应该在哪里定义使用ember-cli生成的Ember JS应用中的灯具?我尝试了很多地方,比如app.js在一个名为"灯具"的文件夹中.
在DS.Store#pushPayload调用之后是否有更好的方法来返回记录?这就是我正在做的......
var payload = { id: 1, title: "Example" }
store.pushPayload('post', payload);
return store.getById('post', payload.id);
Run Code Online (Sandbox Code Playgroud)
但是,定期DS.Store#push返回插入的记录.根据我的判断,两者之间的唯一区别是DS.Store#pushPayload使用正确的序列化器序列化有效负载数据.
我正在尝试使用记录的model.reload()函数轮询更多数据
App.ModelViewRoute = Ember.Route.extend({
actions: {
reload: function() {
this.get('model').reload();
}
}
});
Run Code Online (Sandbox Code Playgroud)
但是我收到一条错误消息说......
undefined is not a function TypeError: undefined is not a function
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做到这一点,似乎我无法以这种方式从路线访问模型?
这是路由器
App.Router.map(function() {
this.route('video', { path: '/videos/:video_id' });
});
Run Code Online (Sandbox Code Playgroud)
这是路线
App.VideoRoute = Ember.Route.extend({
model: function(params) {
return this.store.find('video', params.video_id);
},
actions: {
reloadModel: function() {
// PROBLEM HERE
// this.get('model').reload();
Ember.Logger.log('reload called!');
}
}
});
Run Code Online (Sandbox Code Playgroud)
这是模型
App.Video = DS.Model.extend({
title: DS.attr('string'),
status: DS.attr('string')
});
Run Code Online (Sandbox Code Playgroud)
和模板
<script type="text/x-handlebars" data-template-name="application">
<h1>Testing model reloading</h1>
{{#link-to "video" 1}}view …Run Code Online (Sandbox Code Playgroud) 在Ember 2+中,是否有人知道如何获取对Ember Store的引用以便在javascript控制台中对模型映射进行故障排除?
通过App.__container__.lookupEmber 1 可以实现,但这已经不存在了,而且很难找到相关的文档.
谢谢