Ember.js将现有记录复制到商店 [英] Ember.js Clone an existing record into the store

查看:100
本文介绍了Ember.js将现有记录复制到商店的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力与Ember.js想要的概念。我想要的是以下。我现在有一个名为文章的Ember Data模型。让我们说, id 的1将显示在 / article / 1



当用户点击新建按钮时,它们将转换到article.new路由。看到我的路由器:

  App.Router.map(function(){
this.resource('article' {path:'/ article /:id'});
this.resource('article.new',{path:/ article / new});
}

当用户点击重复按钮时,它们位于 / article / 1 duplicateArticle action被调用。我直观地在 App.ArticleController 中执行以下操作:

  duplicateArticle:function(){
return this.transitionToRoute('article.new',this.get模型');
}

但是这不行,我想是因为我在我的article.new路由中需要一条路径,但是当用户点击新建按钮时,我不需要一个ID。



这个问题是否有效的解决方案?



编辑:我的尝试到目前为止:

  var currentArticle = this.get('model'); 
currentArticle.set('id',null);
var duplicateatedArticle = this.store.createRecord('article',currentArticle);
duplicateatedArticle.save();

和:

 code> var duplicateatedArticle = Ember.copy(this.get('model'),true); 

和:

 code> var newArticle = JSON.parse(JSON.stringify(this.get('model'))); 
var duplicateatedArticle = this.store.createRecord('article',newArticle);
duplicateatedArticle.save();

最后一次尝试工作除了 belongsTo hasMany 属性。



有没有Ember的方法?



参考资料



新记录的Ember克隆模型



有没有办法将Ember Object转换为纯粹的JavaScript对象?



迭代Ember.js ember-数据记录数组



如何克隆一个Ember数据记录,包括关系?(没有回答,75%相同的任务离开为我)





更新:一个简单的例子,也 belongsTo 已保存的项目



hasMany不起作用。如果您有解决方案,请为此回答!



我的最终解决方案没有多项目现在如下:



在我的ArticleController的操作中:

  duplicateArticle:function(){
var article = this .get('model')。toJSON(),
self = this;

// todo实现保存hasMany项目

//手动设置belongsTo项目
文章['landcode'] = this.get('landcode');
article ['employee'] = this.get('employee');
article ['cross_selling_article_relation'] = this.get('cross_selling_article_relation');

var duplicateatedArticle = this.store.createRecord('article',article);

//保存并转移到新创建的文章
duplicateatedArticle.save()然后(function(savedArticle){
self.transitionToRoute('article',savedArticle.id) ;
});
},


解决方案

Ember Data is in beta ,所以缺少的功能是预期的,只有通过查找解决时,才能通过ids来解决关系。



如果您创建一个虚拟ID,您可以pushPayload然后找到。



W / o关系,您可以使用record.toJSON()获取w / o id的属性,然后发送到createRecord。



http://emberjs.jsbin.com/OxIDiVU/13/edit



您应该提交一个PR具有重复的逻辑,这将是对社区做出贡献的好方法。


I am struggling with the concept Ember.js will like. What I want is the following. I have now an existing Ember Data model called Article. Lets say with the id of 1 will be shown on /article/1.

When the user hit the New button they are transitioned to the 'article.new' route. See my routers:

App.Router.map(function () {
   this.resource('article', {path: '/article/:id'});
   this.resource('article.new', {path: "/article/new"});
}

When the user click the Duplicate button when they are at /article/1 the duplicateArticle action gets called. I intuitively do the following in App.ArticleController:

duplicateArticle: function () {
   return this.transitionToRoute('article.new', this.get('model');
}

However that is not going to work. I think because I need an path in my article.new route. However, when a user click on the New button I do not need an ID.

Is there a valid solution to this question?

Edit: My tries so far:

var currentArticle = this.get('model'); 
currentArticle.set('id', null); 
var duplicatedArticle = this.store.createRecord('article', currentArticle); 
duplicatedArticle.save();

and:

var duplicatedArticle = Ember.copy(this.get('model'), true);

and:

 var newArticle = JSON.parse(JSON.stringify(this.get('model')));
 var duplicatedArticle = this.store.createRecord('article', newArticle);
 duplicatedArticle.save();

The last try does work except for belongsTo and hasMany properties.

Is there no Ember way of doing this?

References:

Ember clone model for new record

Is there any way to convert Ember Object into plain javascript object?

Iterating over Ember.js ember-data Record Arrays

How can I clone an Ember Data record, including relationships? (not answered, 75% the same question as me)

.

Update: a simple example with also belongsTo items saved

hasMany does not work. Contribute to this answer if you have a solution!

My final solution without hasMany items is now as follows:

In my actions of my 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);
        });
    },

解决方案

Ember Data is in beta, so missing functionality is to be expected, relationships are only resolved from ids when resolved through find.

If you create a dummy id you can pushPayload then find.

W/o relationships, you can use record.toJSON() to get the attributes w/o id, then send it to createRecord.

http://emberjs.jsbin.com/OxIDiVU/13/edit

You should submit a PR with duplicate logic, it would be a great way to contribute to the community.

这篇关于Ember.js将现有记录复制到商店的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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