大佬教程收集整理的这篇文章主要介绍了ember.js – 如何克隆Ember数据记录,包括关系?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
作为参考,这里是我将克隆Ember数据记录的属性:
var attributeKeys = oldModel.get('constructor.attributes.keys.list'); var newRecord = this.get('store').createRecord(oldModel.constructor.typeKey); newRecord.setProperties(oldModel.getProperties(attributeKeys));
Ds.Model.reopen({ clone: function(overrides) { var model = this,attrs = model.toJSON(),class_type = model.constructor; var root = Ember.String.decamelize(class_type.toString().split('.')[1]); /* * Need to replace the belongsTo association ( id ) with the * actual model instance. * * For example if belongsTo association is project,the * json value for project will be: ( project: "project_id_1" ) * and this needs to be converted to ( project: [projecTinstance] ) */ this.eachRelationship(function(key,relationship) { if (relationship.kind == 'belongsTo') { attrs[key] = model.get(key); } }); /* * Need to dissociate the new record from the old. */ delete attrs.id; /* * Apply overrides if provided. */ if (Ember.typeOf(overrides) === 'object') { Ember.setProperties(attrs,overrides); } return this.store.createRecord(root,attrs); } });
以上是大佬教程为你收集整理的ember.js – 如何克隆Ember数据记录,包括关系?全部内容,希望文章能够帮你解决ember.js – 如何克隆Ember数据记录,包括关系?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。