在Backbone.js的嵌套模型,如何处理 [英] Nested Models in Backbone.js, how to approach

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

问题描述

我得从服务器提供了以下JSON。有了这个,我想创建一个嵌套模式的典范。我不确定这是实现这一目标的方式。

  // JSON
[{
    名称:榜样,
    布局:{
        X:100,
        Y:100,
    }
}]

我希望这些被转换为两个嵌套骨干模型具有以下结构:

  //结构
图片
    布局
...

所以我定义布局模型,像这样:

  VAR布局= Backbone.Model.extend({});

但其中两个(如果有的话)以下的技术,我应该用它来定义影像模式? A或B下面?

A

  VAR图像= Backbone.Model.extend({
    初始化:功能(){
        this.set({'布局',新的布局(this.get('布局'))})
    }
});

  VAR图像= Backbone.Model.extend({
    初始化:功能(){
        this.layout =新的布局(this.get('布局'));
    }
});


解决方案

我有同样的问题,而我写我的骨干应用。不必处理嵌入式/嵌套模型。我做了一些调整,我认为是一个很优雅的解决方案。

是的,你可以修改解析方法在对象周围改变一个属性,但所有这一切实际上是IMO pretty难以维护code,感觉更多的是黑客比的解决方案。

下面是我的建议为你的例子:

首先定义你的布局模型,像这样。

  VAR layoutModel = Backbone.Model.extend({});

然后这里是你的形象型号:

  VAR imageModel = Backbone.Model.extend({    型号:{
        布局:layoutModel,
    },    解析:功能(响应){
        对(在this.model VAR键)
        {
            VAR embeddedClass = this.model [关键]
            VAR embeddedData =响应[关键]
            响应[关键] =新embeddedClass(embeddedData,{解析:真});
        }
        返回响应;
    }
});

请注意,我还没有与模型本身篡改,只不过从parse方法传回所需的对象。

这应确保嵌套模型的结构,当你从服务器读取。现在,因为我觉得这是有道理的,你要明确使用合适的型号设置嵌套的模型,你会发现,储蓄或设置实际上并不在这里处理。

像这样:

  image.set({布局,新布局({X:100,Y:100})})

也注意到,你实际上是通过调用调用你的嵌套的模型解析方法:

 新embeddedClass(embeddedData,{解析:真});

因为您可以根据需要在模式字段定义为许多嵌套模型。

当然,如果你想尽可能保存在自己的表嵌套模式去。这是不够的。但在阅读和保存对象作为一个整体的情况下,此解决方案应该是足够的。

I've got the following JSON provided from a server. With this, I want to create a model with a nested model. I am unsure of which is the way to achieve this.

//json
[{
    name : "example",
    layout : {
        x : 100,
        y : 100,
    }
}]

I want these to be converted to two nested backbone models with the following structure:

// structure
Image
    Layout
...

So I define the Layout model like so:

var Layout = Backbone.Model.extend({});

But which of the two (if any) techniques below should I use to define the Image model? A or B below?

A

var Image = Backbone.Model.extend({
    initialize: function() {
        this.set({ 'layout' : new Layout(this.get('layout')) })
    }
});

or, B

var Image = Backbone.Model.extend({
    initialize: function() {
        this.layout = new Layout( this.get('layout') );
    }
});

解决方案

I have the very same issue while I'm writing my Backbone application. Having to deal with embedded/nested models. I did some tweaks that I thought was a quite elegant solution.

Yes, you can modify the parse method to change a attributes around in the object, but all of that is actually pretty unmaintainable code IMO, and feels more of a hack than a solution.

Here's what I suggest for your example:

First define your Layout Model like so.

var layoutModel = Backbone.Model.extend({});

Then here's your image Model:

var imageModel = Backbone.Model.extend({

    model: {
        layout: layoutModel,
    },

    parse: function(response){
        for(var key in this.model)
        {
            var embeddedClass = this.model[key];
            var embeddedData = response[key];
            response[key] = new embeddedClass(embeddedData, {parse:true});
        }
        return response;
    }
});

Notice that I have not tampered with the model itself, but merely pass back the desired object from the parse method.

This should ensure the structure of the nested model when you're reading from the server. Now, you would notice that saving or setting is actually not handled here because I feel that it makes sense for you to set the nested model explicitly using the proper model.

Like so:

image.set({layout : new Layout({x: 100, y: 100})})

Also take note that you are actually invoking the parse method in your nested model by calling:

new embeddedClass(embeddedData, {parse:true});

You can define as many nested models in the model field as you need.

Of course, if you want to go as far as saving the nested model in its own table. This wouldn't be sufficient. But in the case of reading and saving the object as a whole, this solution should suffice.

这篇关于在Backbone.js的嵌套模型,如何处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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