Del*_*ynx 5 ember.js ember-data
我正在努力与Ember.js想要的概念.我想要的是以下内容.我现在有一个现有的Ember Data模型Article.让我们说id1将会显示/article/1.
当用户点击"新建"按钮时,它们将转换为"article.new"路线.看我的路由器:
App.Router.map(function () {
this.resource('article', {path: '/article/:id'});
this.resource('article.new', {path: "/article/new"});
}
Run Code Online (Sandbox Code Playgroud)
当用户在操作时单击" 复制"按钮时,将调用/article/1该duplicateArticle操作.我直观地做了以下几点App.ArticleController:
duplicateArticle: function () {
return this.transitionToRoute('article.new', this.get('model');
}
Run Code Online (Sandbox Code Playgroud)
然而,这是行不通的.我想因为我需要一条路径在我的文章中.新路线.但是,当用户单击"新建"按钮时,我不需要ID.
这个问题有效吗?
编辑:到目前为止我的尝试:
var currentArticle = this.get('model');
currentArticle.set('id', null);
var duplicatedArticle = this.store.createRecord('article', currentArticle);
duplicatedArticle.save();
Run Code Online (Sandbox Code Playgroud)
和:
var duplicatedArticle = Ember.copy(this.get('model'), true);
Run Code Online (Sandbox Code Playgroud)
和:
var newArticle = JSON.parse(JSON.stringify(this.get('model')));
var duplicatedArticle = this.store.createRecord('article', newArticle);
duplicatedArticle.save();
Run Code Online (Sandbox Code Playgroud)
除了belongsTo和hasMany属性之外,最后一次尝试都有效.
这样做没有Ember方式吗?
参考文献:
有没有办法将Ember Object转换为普通的javascript对象?
如何克隆Ember数据记录,包括关系?(没有回答,75%和我一样的问题)
.
hasMany不起作用.如果您有解决方案,请为此答案做出贡献!
我没有hasMany项目的最终解决方案现在如下:
在我的ArticleController的行动中:
duplicateArticle: function () {
var article = this.get('model').toJSON(),
self = this;
// todo implement saving hasMany items
// set belongsTo items by hand
article['landcode'] = this.get('landcode');
article['employee'] = this.get('employee');
article['cross_selling_article_relation'] = this.get('cross_selling_article_relation');
var duplicatedArticle = this.store.createRecord('article', article);
// save and transite to newly created article
duplicatedArticle.save().then(function (savedArticle) {
self.transitionToRoute('article', savedArticle.id);
});
},
Run Code Online (Sandbox Code Playgroud)
Ember Data 处于测试阶段,因此缺少功能是可以预料的,只有通过 find 解析时才能从 ids 解析关系。
如果您创建一个虚拟 ID,您可以 PushPayload 然后找到。
没有关系,您可以使用 record.toJSON() 获取没有 id 的属性,然后将其发送到 createRecord。
http://emberjs.jsbin.com/OxIDiVU/13/edit
您应该提交具有重复逻辑的 PR,这将是为社区做出贡献的好方法。
| 归档时间: |
|
| 查看次数: |
5091 次 |
| 最近记录: |