Skip to content

Instantly share code, notes, and snippets.

@grantges

grantges/book.js Secret

Created May 1, 2014 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grantges/28241878e3886c1cec51 to your computer and use it in GitHub Desktop.
Save grantges/28241878e3886c1cec51 to your computer and use it in GitHub Desktop.
Duplication Calls to Set Model Data in Alloy View Binding
exports.definition = {
config: {
columns: {
"title": "string",
"author": "string"
},
adapter: {
type: "sql",
collection_name: "book"
}
},
extendModel: function(Model) {
_.extend(Model.prototype, {
// extended functions and properties go here
});
return Model;
},
extendCollection: function(Collection) {
_.extend(Collection.prototype, {
// extended functions and properties go here
});
return Collection;
}
};
var model = Alloy.Models.book;
function doClick(e) {
model.set("title", "Atlas Shrugged");
model.set("author", "Ayn Rand");
}
$.index.open();
<Alloy>
<Model src="book" />
<Window class="container" layout="vertical">
<Button onClick="doClick">Click Me</Button>
<View>
<Label id="title" onClick="doClick" text="{book.title}" />
<Label id="author" onClick="doClick" text="{book.author}" />
</View>
</Window>
</Alloy>
function Controller() {
function doClick() {
model.set("title", "Atlas Shrugged");
model.set("author", "Ayn Rand");
}
require("alloy/controllers/BaseController").apply(this, Array.prototype.slice.call(arguments));
this.__controllerPath = "index";
arguments[0] ? arguments[0]["__parentSymbol"] : null;
arguments[0] ? arguments[0]["$model"] : null;
arguments[0] ? arguments[0]["__itemTemplate"] : null;
var $ = this;
var exports = {};
var __defers = {};
Alloy.Models.instance("book");
$.__views.index = Ti.UI.createWindow({
backgroundColor: "white",
layout: "vertical",
id: "index"
});
$.__views.index && $.addTopLevelView($.__views.index);
$.__views.__alloyId2 = Ti.UI.createButton({
title: "Click Me",
id: "__alloyId2"
});
$.__views.index.add($.__views.__alloyId2);
doClick ? $.__views.__alloyId2.addEventListener("click", doClick) : __defers["$.__views.__alloyId2!click!doClick"] = true;
$.__views.__alloyId3 = Ti.UI.createView({
id: "__alloyId3"
});
$.__views.index.add($.__views.__alloyId3);
$.__views.title = Ti.UI.createLabel({
width: Ti.UI.SIZE,
height: Ti.UI.SIZE,
color: "#000",
id: "title"
});
$.__views.__alloyId3.add($.__views.title);
doClick ? $.__views.title.addEventListener("click", doClick) : __defers["$.__views.title!click!doClick"] = true;
$.__views.author = Ti.UI.createLabel({
width: Ti.UI.SIZE,
height: Ti.UI.SIZE,
color: "#000",
id: "author"
});
$.__views.__alloyId3.add($.__views.author);
doClick ? $.__views.author.addEventListener("click", doClick) : __defers["$.__views.author!click!doClick"] = true;
/************************************************************************************
* HERE IS THE GENERATED FUNCTION FOR THE BINDING
* Notice the duplicate calls to set the text property on the corresponding labels
************************************************************************************/
var __alloyId4 = function() {
$.title.text = _.isFunction(Alloy.Models.book.transform) ? Alloy.Models.book.transform()["title"] : Alloy.Models.book.get("title");
$.title.text = _.isFunction(Alloy.Models.book.transform) ? Alloy.Models.book.transform()["title"] : Alloy.Models.book.get("title");
$.author.text = _.isFunction(Alloy.Models.book.transform) ? Alloy.Models.book.transform()["author"] : Alloy.Models.book.get("author");
$.author.text = _.isFunction(Alloy.Models.book.transform) ? Alloy.Models.book.transform()["author"] : Alloy.Models.book.get("author");
};
Alloy.Models.book.on("fetch change destroy", __alloyId4);
exports.destroy = function() {
Alloy.Models.book.off("fetch change destroy", __alloyId4);
};
_.extend($, $.__views);
var model = Alloy.Models.book;
$.index.open();
__defers["$.__views.__alloyId2!click!doClick"] && $.__views.__alloyId2.addEventListener("click", doClick);
__defers["$.__views.title!click!doClick"] && $.__views.title.addEventListener("click", doClick);
__defers["$.__views.author!click!doClick"] && $.__views.author.addEventListener("click", doClick);
_.extend($, exports);
}
var Alloy = require("alloy"), Backbone = Alloy.Backbone, _ = Alloy._;
module.exports = Controller;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment