递归函数的jQuery与骨干 [英] Recursive function jquery with backbone

查看:89
本文介绍了递归函数的jQuery与骨干的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在骨干网的应用程序,我想找到一个JSON一些记录里面打印出来。

I have an app in backbone where I want to find inside a Json some records and print out.

我的JSON是这样的:

my JSON is like this:

[
  {
    "id" : "r1",
    "hotel_id" : "1",
    "name" : "Single",
    "level" : "1"
  },
  {
    "id" : "r1_1",
    "hotel_id" : "1",
    "name" : "Double",
    "level" : "2"
  },
  {
    "id" : "r1_3",
    "hotel_id" : "1",
    "name" : "Double for single",
    "level" : "1"
  },
  {
    "id" : "r1_4",
    "hotel_id" : "1",
    "name" : "Triple",
    "level" : "3"
  },
  {
    "id" : "r2",
    "hotel_id" : "2",
    "name" : "Single",
    "level" : "1"
  },
  {
    "id" : "r2_1",
    "hotel_id" : "2",
    "name" : "Triple",
    "level" : "1"
  }
]

我想每个房间的每家酒店的级别相结合。每家酒店能有更多的房间组合,但独特的水平。
我的目标是打印这样的酒店,其中ID = 1(同为其他不同的组合):
酒店第一组合ID为1:结果

I want to combine each room for each hotel for level. Each hotel can have more rooms combination but unique level. My goal is to print something like this for hotel where id = 1 (same for the other with different combination): First combination for hotel with id 1:

Room "Single", "level" : "1" , "hotel_id" : "1"
Room "Double", "level" : "2" , , "hotel_id" : "1"
Room "Triple", "level" : "3" , , "hotel_id" : "1"

id为1家酒店第二组合:结果

Second combination for hotel with id 1:

Room "Double for single", "level" : "1" , "hotel_id" : "1"
Room "Double", "level" : "2" , , "hotel_id" : "1"
Room "Triple", "level" : "3" , , "hotel_id" : "1"

每个酒店可以有一定程度的更多的房间,但我想构建具有一个房间的foreach酒店组合。

Each hotel can have more rooms of some level, but I want to construct combination with one room foreach hotel.

这是我在骨干解析,但我只检索JSON里面allRooms。

This is my parsing in backbone but I have only retrieve the JSON inside allRooms.

//each for all my hotel
_.each(this.collection.models, function(hotel) {
   var rooms = new Array();
   rooms.push(allRooms.where({hotel_id : hotel.id}));

   //this is where I have to construct my combination

   //this is the array for each combination
   hotel.get('rooms').push(rooms);
});

如何构建这个组合?

How to construct this combination?

推荐答案

根据@ BERGI的答案,我想出了这个。它应该解决您的问题。

Based on @Bergi's answer, I came up with this. It should solve your problem.

下面是一个演示: http://plnkr.co/edit/NHE9V5? p = preVIEW

更新我修改了一些东西,以适应您不同的JSON文件。

Updated I have modified some things to accomodate your separate JSON files.

笛卡尔乘积助手 http://en.wikipedia.org/wiki / Cartesian_product

function cartesian(arg) {
  arg = arg || [];
  var r = [],
        max = arg.length - 1;

    function helper(arr, i) {
        for (var j = 0, l = arg[i].length; j < l; j++) {
            var a = arr.slice(0); // clone arr
            a.push(arg[i][j]);
            if (i == max) {
                r.push(a);
            } else helper(a, i + 1);
        }
    }
  if(arg.length > 0)
      helper([], 0);
    return r;
}

嵌套收集解决方案

HotelModel = Backbone.Model.extend({
    initialize: function() {
        // because initialize is called after parse
        _.defaults(this, {
            rooms: new RoomCollection()
        });
    },
    parse: function(response) {
        if (_.has(response, "rooms")) {
            this.rooms = new RoomCollection(response.rooms, {
                parse: true
            });
            delete response.rooms;
        }
        return response;
    },
    toJSON: function() {
        var json = _.clone(this.attributes);
        json.rooms = this.rooms.toJSON();
        return json;
    },
    addRoom: function(rooms, options) {
        return this.rooms.add(rooms, options);
    },
    removeRoom: function(rooms, options) {
        return this.rooms.remove(rooms, options);
    },
    createRoom: function(attributes, options) {
        return this.rooms.create(attributes, options);
    },
    getCombinations: function() {
        return cartesian(_.values(this.rooms.groupBy('level')));
    }
});

RoomModel = Backbone.Model.extend({});

HotelCollection = Backbone.Collection.extend({
    model: HotelModel,
  getAllCombinations: function(){
    return this.map(function(hotel){
      return _.extend(hotel.toJSON(), {
        combinations: hotel.getCombinations()
      });
    });
  }
});

RoomCollection = Backbone.Collection.extend({
    model: RoomModel,
    getRoomsByHotelId: function(hotelId) {
        return this.where({
            hotelId: hotelId
        });
    }
});

的独立JSON的装载

var hotels = new HotelCollection([], {
    url: 'hotels.json'
});
var rooms = new RoomCollection([], {
    url: 'rooms.json'
});

hotels.fetch({
    success: function() {
        rooms.fetch({
            success: function() {
                hotels.each(function(hotel) {
                    hotel.addRoom(rooms.getRoomsByHotelId(hotel.id));
                });
                // all done here
                var combos = hotels.getAllCombinations();
                $(function() {
                    $('body').append('<pre>' + JSON.stringify(combos, null, 2) + '</pre>');
                });
            }
        });
    }
});

hotels.json

[{
  "id": 1,
    "name": "Hotel One"
}, {
    "id": 2,
    "name": "Hotel Two"
}, {
    "id": 3,
    "name": "Hotel Three"
}]

rooms.json

[{
  "level": 1,
    "name": "Room A",
    "hotelId": 1
}, {
    "level": 1,
    "name": "Room B",
    "hotelId": 1
}, {
    "level": 2,
    "name": "Room A",
    "hotelId": 1
}, {
    "level": 2,
    "name": "Room B",
    "hotelId": 1
}, {
    "level": 1,
    "name": "Room A",
    "hotelId": 2
}, {
    "level": 1,
    "name": "Room B",
    "hotelId": 2
}, {
    "level": 2,
    "name": "Room A",
    "hotelId": 2
}, {
  "level": 2,
    "name": "Room B",
    "hotelId": 2
}, {
  "level": 1,
  "name": "Room A",
    "hotelId": 3
}, {
  "level": 1,
  "name": "Room B",
  "hotelId": 3
}, {
  "level": 1,
  "name": "Room C",
  "hotelId": 3
}]

这篇关于递归函数的jQuery与骨干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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