2014-12-05 43 views
0

我有一個Backbone集合,其中包含畫布X和Y座標及其寬度和高度(矩形)。當我點擊畫布元素時,如果點擊座標位於Xcoordinate + widthYcoordinate+height之間,我必須在模型中搜索,但我找不到用><條件搜索集合的方法。主幹搜索條件

是否有可能使用這些條件搜索Backbone集合? collection.where只允許=條件。

型號

Model = Backbone.Model.extend({ 
    defaults: { 
     LayoutConfigurationId: 0,    
     Xcoord : 0, 
     Ycoord : 0,    
     Width : 0,    
     Height : 0    
    }, 
    initialize: function (model) { 
     contextMicro.clearRect(model.Xcoord-3, model.Ycoord-3, model.Width+6, model.Height+6); 

     contextMicro.beginPath(); 
     contextMicro.rect(model.Xcoord, model.Ycoord, model.Width, model.Height); 
     contextMicro.fillStyle = 'red'; 
     contextMicro.fill(); 
     contextMicro.lineWidth = 2; 
     contextMicro.strokeStyle = 'black'; 
     contextMicro.stroke(); 

    } 
}); 

收集

var Collection = Backbone.Collection.extend({ 
    defaults: { 
     model: Model 
    }, 
    model: MicrophoneModel,  

}); 

回答

1

骨幹proxies a lot of Underscore functions到集合並且特別_.filter,讓你寫你能想到的任何過濾功能。

例如,獲得包含給定的點x的所有模型的列表,Y:

var c = new Backbone.Collection([ 
    {Xcoord:10, Ycoord:10, Width:10, Height:10}, 
    {Xcoord:20, Ycoord:20, Width:10, Height:10} 
]); 

var x = 15, y = 15; 
var found = c.filter(function(m) { 
    return (m.get('Xcoord')<x) && ((m.get('Xcoord')+m.get('Width'))>x) 
     && (m.get('Ycoord')<y) && ((m.get('Ycoord')+m.get('Height'))>y); 
}); 

http://jsfiddle.net/nikoshr/wa5xshoa/

+0

就是這樣。謝謝。 – 2014-12-05 13:53:58