主干模型:嵌套数据结构 [英] Backbone model: nested data structure

查看:232
本文介绍了主干模型:嵌套数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发的应用程序的骨干,这使得跨域宁静的请求。在请求中的嵌套数据结构是必需的,在袅袅的请求我有一个结构:

  {
    SITE_ID:1,
    邮报:{
        SITE_ID:1,
        provider_id:1,
        provider_post_id:1,
        created_ts:12.12.12,
        邮报:{
            头:文本,
            说明:文本,
            形象:HTTP://...jpg
        }
    }
}

在模型中我没有嵌套结构,这是pretty舒服,因为我使用了视图图片示范田code>(DOM元素创建)。

什么是正确的方式,从骨干网应用嵌套数据发送到服务器?

型号:

  VAR WraperModel = Backbone.Model.extend({
    网址:HTTP:// MYDOMAIN /核心/ API / V1 / bookmarklet_post /?回调=?,
    默认值:{
        SITE_ID:1,//应该不难codeD
        类型:类型,SITE_ID:2,provider_id:2,provider_post_id:2,created_ts:2,
        标题:'',
        标题:'',
        图片: ''
    },
});

来看,其中一部分用图片模型属性:

  DRAWITEM:函数(模型){
    VAR研究所=新ImageView的({模式:模式,标签名:礼,类名:图像项'})渲染();
    this.imagesWrapper.append(inst.el);
},
getImages:功能(){
   VAR图像= doc.getElementsByTagName('IMG'),
       鉴于此=;
   _.each(图片,功能(图像){
       image.offsetHeight> 75
       &功放;&安培; image.offsetWidth> 75安培;&安培;
       view.collection.add({图片:image.src});
   });
},

另一种观点认为,这将数据发送到服务器的一部分。

  SENDTO:功能(){
    VAR是这个=,
    数据= {节能:真正};    $('#附加头)VAL()及和放大器; (data.header = $('#附加头)VAL());
    $('#附加说明)VAL()及和放大器; (data.caption = $('#附加说明)VAL());
    this.model.set(数据);
    this.model.save();
}


解决方案

您在通过保存第一个参数是将要传承下去的属性的哈希在你保存。

在你的 SENDTO 函数只是建立从模型的数据和任何其他形式的值的对象,该服务器期望的一种方式。默认情况下,当保存成功从响应该数据将通过解析方法来传递,并设置回模型。

  VAR MYDATA的= {
     //为服务器创建嵌套对象
};this.model.save(MYDATA的);

I'm developing backbone app, which makes crossdomain restful request. The nested data structure in request are required, in the curl request I have that structure:

{
    "site_id": 1,
    "post": {
        "site_id": 1,
        "provider_id": 1,
        "provider_post_id":1,
        "created_ts": "12.12.12",
        "post": {
            "header": "text",
            "caption": "text",
            "image": "http://...jpg"
        }
    }
}

In the model I have not nested structure and this is pretty comfortable, because I use image model field in the view (DOM element creation).

What the correct way to send nested data to server from Backbone app?

Model:

var WraperModel = Backbone.Model.extend({
    url: 'http://mydomain/core/api/v1/bookmarklet_post/?  callback=?',
    defaults: {
        site_id: 1, // shouldn't be hardcoded
        type:"type", site_id:2, provider_id: 2, provider_post_id: 2,  created_ts:2,
        header : '',
        caption: '',
        image: ''
    },
});

The part of view, which use image model property:

drawItem: function (model) {
    var inst = new ImageView({model: model, tagName: 'li',     className:'images-item'}).render();
    this.imagesWrapper.append(inst.el);
},
getImages: function () {
   var images = doc.getElementsByTagName('img'),
       view = this;
   _.each(images, function (image) {
       image.offsetHeight > 75 
       && image.offsetWidth > 75 &&
       view.collection.add({image: image.src});
   });
},

The part of another view, which send data to server.

sendTo: function(){
    var that = this,
    data = {saving: true};

    $('#add-header').val() && (data.header = $('#add-header').val());
    $('#add-description').val() && (data.caption = $('#add-description').val());
    this.model.set(data);
    this.model.save();
}

解决方案

the first parameter you pass in save is a hash of the attributes that are going to be passed along in your save.

In your sendTo function just build up an object with the data from your model and any additional form values in a way that the server expects. By default, when a save is successful the data from the response will be passed through the parse method and set back on the model.

var myData = {
     //create nested object for the server
};

this.model.save(myData);

这篇关于主干模型:嵌套数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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