获取父模型的价值与Backbone.js的 [英] Getting value of parent model with Backbone.js

查看:126
本文介绍了获取父模型的价值与Backbone.js的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下结构的主干应用程序:

A PageGroupCollection 是一家集 PageGroupModel 秒。

A PageGroupModel 有一个 PageCollection 作为它的一个属性。

A PageCollection 是一家集 PageModel 取值


PageGroupModel 有一个永久属性

PageModel 有一个永久属性。

我需要在 PageModel 函数返回一个永久,它同时包括永久链接 PageModel 的多数民众赞成 PageCollection 属性包含 PageGroupModel 也是自己的永久属性。

类似以下内容:

  getFullPermalink:功能(){    VAR parentPermanlink = this.owningCollection.modelCollectionIsAnAttributeOf.get('永久');    返回parentPermalink +/+ this.get('永久链接');
}

更新:通过添加以下曾任职:

  VAR pageGroup = pageGroupCollection.findWhere({'pageCollection':this.collection});
VAR pageGroupPermalink = pageGroup.get('永久');
返回pageGroupPermalink +/+ this.get('永久链接');


解决方案

骨干是不是真的让嵌套模型(骨干-关系可能是有趣)工作。但是,如果你有机会获得你的树的入口点(在你的榜样,以 PageGroupCollection 的实例),你可以这样做:

  VAR父= myPageGroupCollection.findWhere({pageCollection:myPageModel.collection});

使用 myPageModel 是您的孩子模型和 pageCollection 的集合属性 PageGroupModel

假如你也不要使用属性你的模型混乱(如果你的模型是在几个集合为例)。

I have a backbone application with the following architecture:

A PageGroupCollection is a collection of PageGroupModels.

A PageGroupModel has a PageCollection as one of it's attributes.

A PageCollection is a collection of PageModels


PageGroupModel has a permalink attribute

PageModel has a permalink attribute.

I need a function within PageModel that returns a permalink that includes both the permalink of PageModel thats PageCollection attribute contains PageGroupModel and also its own permalink attribute.

Something like the following:

getFullPermalink: function () {

    var parentPermanlink = this.owningCollection.modelCollectionIsAnAttributeOf.get('permalink');

    return parentPermalink + "/" + this.get('permalink');
}

Update: Worked by adding the following:

var pageGroup = pageGroupCollection.findWhere({ 'pageCollection': this.collection });
var pageGroupPermalink = pageGroup.get('permalink');
return pageGroupPermalink + "/" + this.get('permalink');

解决方案

Backbone isn't really made to work with nested models (Backbone-Relational may be interesting). But if you have access to the entry point of your tree (in your example, to the instance of PageGroupCollection), you can do that:

var parent = myPageGroupCollection.findWhere({pageCollection: myPageModel.collection});

With myPageModel being your child model and pageCollection the collection attribute of the PageGroupModel.

Supposing you also don't mess with the collection attribute of your models (if your model is in several collections for example).

这篇关于获取父模型的价值与Backbone.js的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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