Skip to content

Instantly share code, notes, and snippets.

@rajeshpillai
Forked from mxriverlynn/awful.js
Created April 13, 2012 19:05
Show Gist options
  • Save rajeshpillai/2379248 to your computer and use it in GitHub Desktop.
Save rajeshpillai/2379248 to your computer and use it in GitHub Desktop.
Improve This Backbone "Query"
// A home has floors.
// A floor has rooms.
// A room has device groups.
// A device group has devices.
// A device has addresses.
// An address has actual address value.
//
// So, the purpose of this awful mess is to say :
// "give me every device with this address, from any home"
//
// This is first thing I could come up with that works. But I know it's
// an awful mess. I'd really like to clean it up.
//
// Suggestions, other than use a database?
//
// The data is in a .json file, and I do not have the option of using a database.
Home = Backbone.Model.extend({
// other stuff here
// ...
findDevicesByAddress: function(address){
var devices = new HIT.DeviceCollection();
this.each(function(home){
return home.floors.each(function(floor){
return floor.rooms.each(function(room){
return room.deviceGroups.each(function(deviceGroup){
return deviceGroup.devices.each(function(device){
return device.addresses.each(function(deviceAddr){
var addr = deviceAddr.get("address");
if (addr === address){
devices.add(deviceAddr);
};
});
});
});
});
});
});
return devices;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment