-
-
Save rajeshpillai/2379248 to your computer and use it in GitHub Desktop.
Improve This Backbone "Query"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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