Ember.js 将现有记录克隆到存储中 [英] Ember.js Clone an existing record into the store

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

问题描述

我正在为 Ember.js 喜欢的概念苦苦挣扎.我想要的是以下内容.我现在有一个名为 Article 的现有 Ember 数据模型.假设 id 为 1 将显示在 /article/1 上.

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.

当用户点击 New 按钮时,他们将转换到article.new"路线.查看我的路由器:

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

当用户在 /article/1 处点击 Duplicate 按钮时,duplicateArticle 操作被调用.我在 App.ArticleController 中直观地做了以下操作:

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');
}

然而,这是行不通的.我想是因为我的文章中需要一条路径.新路线.但是,当用户单击新建"按钮时,我不需要 ID.

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?

到目前为止我的尝试:

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

和:

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

和:

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

除了 belongsTohasMany 属性外,最后一次尝试确实有效.

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

Ember 没有办法做到这一点吗?

Is there no Ember way of doing this?

参考:

新记录的 Ember 克隆模型

有什么办法可以将 Ember 对象转换为普通的 javascript 对象?

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

如何克隆 Ember 数据记录,包括关系?(未回答,75% 的问题与我相同)

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

.

hasMany 不起作用.如果您有解决方案,请为这个答案做出贡献!

我没有hasMany项目的最终解决方案如下:

My final solution without hasMany items is now as follows:

在我的 ArticleController 操作中:

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 处于测试阶段,因此缺少功能是可以预料的,当通过 find 解析时,关系仅从 id 解析.

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

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

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

无关系,可以使用record.toJSON()获取无id的属性,然后发送给createRecord.

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

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

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

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

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