如何在主干中使用数组作为id

时间:2013-03-08 05:08:46

标签: backbone.js

在骨干网中,我有一个来自网络服务器的json结构。(注意,我只会收到一堆文件,所以不是为了提供宁静的服务) {'stations':[station1,station2],'geo':[lat1,lng1,lat2,lng2 ....]} ,其中'stations'数组只有两个数字代表公交车站号码,而'geo'代表两个车站之间的中间地理位置。并且通常在应用程序中,我可以在任何数组[station1,station2]或[station2,station1]中获取站点数组,然后我使用此站点数组作为唯一ID来获取'geo'数组 - 中间地理位置点。

例如, {'stations':[38306,38304],'geo':[53.21579073715244,-2.8958864745734445,53.26019,-2.9422970000000532]} ,这意味着我有两个编号为38306和38304的工作站,和4个长度数组'geo'代表2个地理点。

window.stanoxPoints = Backbone.Model.extend({
    defaults:{
        "stations":new Array(),
        "points":new Array()
    }           
});

window.stanoxPointsCollection = Backbone.Collection.extend({
  model: stanoxPoints,
  url:"http://localhost:8080/geo",
});

var stanoxCollection = new stanoxPointsCollection();
stanoxCollection.fetch({
        success:function(collection,response){
            console.log('fetch the data success');
            collection.each(function(data){
                console.log(data.get('stations')+"   "+data.get('points'));                             
            })
        },
        error:function(collection, response){
        }
    })      

所以我只是以上面这种方式设计模型,并使用集合获取数据,但是当我想查询数据时,出现了错误,

stanoxCollection.get([38201,38202])    //undefined

我的问题是,如果我只使用'station'数组来获取'geo'数组,我怎么能把它作为'station'作为骨干模型中的id或index,而不管数组的顺序是什么'station',ex [station1,station2]或[station2,station1]它可以得到地理点数组,只有当站阵列顺序颠倒时,地理数组的顺序也会反转。

现在我尝试了idAttribute:'station',所以既然原始id是[38201,38202],那么  console.log('fetch get by id',stanoxCollection.get([38201,38202])。get('points')); // works  console.log('fetch get by id',stanoxCollection.get([38202,38201])); //不适用于反向数组作为id

有办法解决吗?

0 个答案:

没有答案