2012-07-17 22:22:38 +00:00
|
|
|
|
// js/modules/controls.js
|
|
|
|
|
// Module reference argument, assigned at the bottom
|
|
|
|
|
// every device gets its model from the base and is rendered into seperate viewbases
|
|
|
|
|
|
2012-07-19 14:25:01 +00:00
|
|
|
|
(function (Device) {
|
|
|
|
|
|
2012-07-17 22:22:38 +00:00
|
|
|
|
// Dependencies
|
2012-07-19 14:25:01 +00:00
|
|
|
|
var Position = app.module("position");
|
2012-07-17 22:22:38 +00:00
|
|
|
|
var Option = app.module("option");
|
2012-07-19 14:25:01 +00:00
|
|
|
|
var Util = app.module("util");
|
2012-07-17 22:22:38 +00:00
|
|
|
|
|
|
|
|
|
Device.Model = Backbone.Model.extend({
|
|
|
|
|
defaults: {
|
2012-07-19 14:25:01 +00:00
|
|
|
|
deviceId: 0,
|
|
|
|
|
options: Option.List,
|
|
|
|
|
pos: Position.Model,
|
|
|
|
|
type: ''
|
2012-07-17 22:22:38 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2012-07-19 14:25:01 +00:00
|
|
|
|
Device.List = Backbone.Collection.extend({
|
|
|
|
|
model: Device.Model
|
2012-07-17 22:22:38 +00:00
|
|
|
|
});
|
|
|
|
|
|
2012-07-19 14:25:01 +00:00
|
|
|
|
Device.View = Backbone.View.extend({
|
|
|
|
|
initialize: function () {
|
|
|
|
|
this.$el.addClass('device');
|
2012-07-17 22:22:38 +00:00
|
|
|
|
|
2012-07-19 14:25:01 +00:00
|
|
|
|
this.options = new Option.List(this.model.get('options'));
|
|
|
|
|
this.pos = new Position.Model(this.model.get('pos'));
|
|
|
|
|
this.type = this.model.get('type');
|
2012-07-17 22:22:38 +00:00
|
|
|
|
|
2012-07-19 14:25:01 +00:00
|
|
|
|
this.template = Handlebars.compile(Util.getTemplate(this.type));
|
|
|
|
|
},
|
|
|
|
|
render: function () {
|
|
|
|
|
this.$el.attr('style', 'left:' + this.pos.get('x') + 'px;top:' + this.pos.get('y') + 'px');
|
|
|
|
|
this.$el.html(this.template(this.model.toJSON()));
|
|
|
|
|
return this;
|
|
|
|
|
},
|
|
|
|
|
events: {
|
|
|
|
|
"click .switch": "toggleSwitch"
|
|
|
|
|
},
|
|
|
|
|
toggleSwitch: function () {
|
|
|
|
|
var status = this.model.get('options')[0].status;
|
|
|
|
|
status = (status == "off" ? "on" : "off");
|
|
|
|
|
this.model.get('options')[0].status = status;
|
|
|
|
|
this.model.trigger("change:options");
|
|
|
|
|
this.render();
|
|
|
|
|
}
|
|
|
|
|
});
|
2012-07-17 22:22:38 +00:00
|
|
|
|
|
2012-07-19 14:25:01 +00:00
|
|
|
|
})(app.module("device"));
|