Skip to content

Commit

Permalink
Rename to backrub
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Guimont committed May 6, 2011
1 parent 5fc2b2a commit 0464387
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 86 deletions.
10 changes: 5 additions & 5 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Backbone Template TODOs example
# Backrub TODOs example

Partly copied and adapted from the original example by Jerome https://github.com/jeromegn/localtodos .

# What is Backbone Template
# What is Backrub

Backbone Template lets you describe your Backbone view layer in a single template that will automatically refresh when the bounded data changes. It uses Handlebars and is inspired by SproutCore (>= 1.5) SC.TemplateView .
Backrub lets you describe your Backbone view layer in a single template that will automatically refresh when the bounded data changes. It uses Handlebars and is inspired by SproutCore (>= 1.5) SC.TemplateView .

## Basic use

* Create your template
* Load it in a Backbone.Template object
* Load it in a Backbone.Backrub object
* render it and append it to your page (somewhere, the body maybe)
* Make it alive to start all eventing and make your views behave as intended

Expand All @@ -21,7 +21,7 @@ Backbone Template lets you describe your Backbone view layer in a single templat

# More...

Check out Backbone Template Github repo at https://github.com/juggy/backbone-template
Check out Backrub Github repo at https://github.com/juggy/Backrub

# License (MIT)

Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"></script>
<script type="text/javascript" src="javascripts/backbone.localStorage.js" type="text/javascript"></script>
<script type="text/javascript" src="javascripts/handlebars.js" ></script>
<script type="text/javascript" src="javascripts/backbone_template.js" ></script>
<script type="text/javascript" src="javascripts/backrub.js" ></script>
</head>
<body>
<script id="app-template" type="text/handlebar-template">
Expand Down
80 changes: 40 additions & 40 deletions javascripts/backbone_template.js → javascripts/backrub.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(function() {
var Template, proto, _i, _len, _ref;
Template = {
var Backrub, proto, _i, _len, _ref;
Backrub = {
_Genuine: {
nameLookup: Handlebars.JavaScriptCompiler.prototype.nameLookup,
mustache: Handlebars.Compiler.prototype.mustache
Expand Down Expand Up @@ -30,15 +30,15 @@
},
_resolveValue: function(attr, model) {
var model_info, value;
model_info = Template._resolveIsModel(attr, model);
model_info = Backrub._resolveIsModel(attr, model);
if (model_info.is_model) {
return model_info.model.get(model_info.attr);
} else if (model_info.is_model === null) {
return attr;
} else {
value = (function() {
try {
return Template._getPath(model_info.attr, model_info.model, true);
return Backrub._getPath(model_info.attr, model_info.model, true);
} catch (error) {

}
Expand Down Expand Up @@ -68,8 +68,8 @@
_bindIf: function(attr, context) {
var model_info, view;
if (context) {
view = Template._createBindView(attr, this, context);
model_info = Template._resolveIsModel(attr, this);
view = Backrub._createBindView(attr, this, context);
model_info = Backrub._resolveIsModel(attr, this);
model_info.bind(function() {
if (context.data.exec.isAlive()) {
view.rerender();
Expand All @@ -78,7 +78,7 @@
});
view.render = function() {
var fn;
fn = Template._resolveValue(this.attr, this.model) ? context.fn : context.inverse;
fn = Backrub._resolveValue(this.attr, this.model) ? context.fn : context.inverse;
return new Handlebars.SafeString(this.span(fn(this.model, {
data: context.data
})));
Expand All @@ -95,8 +95,8 @@
self = model || this;
_.each(attrs, function(attr, k) {
var model_info, value;
model_info = Template._resolveIsModel(attr, self);
value = Template._resolveValue(attr, self);
model_info = Backrub._resolveIsModel(attr, self);
value = Backrub._resolveValue(attr, self);
outAttrs.push("" + k + "=\"" + value + "\"");
return model_info.bind(function() {
var el;
Expand All @@ -105,7 +105,7 @@
if (el.length === 0) {
return model_info.model.unbind("change" + model_info.attr);
} else {
return el.attr(k, Template._resolveValue(attr, self));
return el.attr(k, Backrub._resolveValue(attr, self));
}
}
});
Expand All @@ -121,16 +121,16 @@
if (!v) {
throw new Error("Cannot instantiate view");
}
v._ensureElement = Template._BindView.prototype._ensureElement;
v.span = Template._BindView.prototype.span;
v.live = Template._BindView.prototype.live;
v.textAttributes = Template._BindView.prototype.textAttributes;
v._ensureElement = Backrub._BindView.prototype._ensureElement;
v.span = Backrub._BindView.prototype.span;
v.live = Backrub._BindView.prototype.live;
v.textAttributes = Backrub._BindView.prototype.textAttributes;
v.bvid = "" + (_.uniqueId('bv'));
return v;
},
_createBindView: function(attr, model, context) {
var view;
view = new Template._BindView({
view = new Backrub._BindView({
attr: attr,
model: model,
context: context,
Expand Down Expand Up @@ -160,7 +160,7 @@
return this.hbContext = this.options.context;
},
value: function() {
return Template._resolveValue(this.attr, this.model);
return Backrub._resolveValue(this.attr, this.model);
},
textAttributes: function() {
this.attributes = this.attributes || this.options.attributes || {};
Expand All @@ -170,7 +170,7 @@
if (!this.attributes["class"] && this.className) {
this.attributes["class"] = this.className;
}
return Template._bindAttr(this.attributes, this.hbContext, this.prevThis || this).string;
return Backrub._bindAttr(this.attributes, this.hbContext, this.prevThis || this).string;
},
span: function(inner) {
return "<" + this.tagName + " " + (this.textAttributes()) + " data-bvid=\"" + this.bvid + "\">" + inner + "</" + this.tagName + ">";
Expand All @@ -186,18 +186,18 @@
Handlebars.Compiler.prototype.mustache = function(mustache) {
var id;
if (mustache.params.length || mustache.hash) {
return Template._Genuine.mustache.call(this, mustache);
return Backrub._Genuine.mustache.call(this, mustache);
} else {
id = new Handlebars.AST.IdNode(['bind']);
mustache = new Handlebars.AST.MustacheNode([id].concat([mustache.id]), mustache.hash, !mustache.escaped);
return Template._Genuine.mustache.call(this, mustache);
return Backrub._Genuine.mustache.call(this, mustache);
}
};
Handlebars.JavaScriptCompiler.prototype.nameLookup = function(parent, name, type) {
if (type === 'context') {
return "\"" + name + "\"";
} else {
return Template._Genuine.nameLookup.call(this, parent, name, type);
return Backrub._Genuine.nameLookup.call(this, parent, name, type);
}
};
Backbone.dependencies = function(onHash, base) {
Expand All @@ -210,7 +210,7 @@
var attr, e, object, parts, _i, _len, _ref, _results;
parts = event.split(" ");
attr = parts[0];
object = Template._getPath(path, base);
object = Backrub._getPath(path, base);
_ref = parts.slice(1);
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
Expand All @@ -235,7 +235,7 @@
dependencies: Backbone.dependencies
});
}
Backbone.Template = function(template) {
Backbone.Backrub = function(template) {
_.bindAll(this, "addView", "render", "makeAlive", "isAlive");
this.compiled = Handlebars.compile(template, {
data: true,
Expand All @@ -246,7 +246,7 @@
this._alive = false;
return this;
};
_.extend(Backbone.Template.prototype, {
_.extend(Backbone.Backrub.prototype, {
render: function(options) {
var self;
self = this;
Expand Down Expand Up @@ -295,7 +295,7 @@
if (!this.template) {
throw new Error("Template is missing");
}
return this.compile = new Backbone.Template(this.template);
return this.compile = new Backbone.Backrub(this.template);
},
render: function() {
try {
Expand All @@ -310,14 +310,14 @@
Handlebars.registerHelper("view", function(viewName, context) {
var execContext, key, resolvedOptions, v, val, view, _ref;
execContext = context.data.exec;
view = Template._getPath(viewName);
view = Backrub._getPath(viewName);
resolvedOptions = {};
_ref = context.hash;
for (key in _ref) {
val = _ref[key];
resolvedOptions[key] = Template._resolveValue(val, this) || val;
resolvedOptions[key] = Backrub._resolveValue(val, this) || val;
}
v = Template._createView(view, resolvedOptions);
v = Backrub._createView(view, resolvedOptions);
execContext.addView(v);
v.render = function() {
return new Handlebars.SafeString(this.span(context(this, {
Expand All @@ -329,8 +329,8 @@
Handlebars.registerHelper("bind", function(attrName, context) {
var execContext, model_info, view;
execContext = context.data.exec;
view = Template._createBindView(attrName, this, context);
model_info = Template._resolveIsModel(attrName, this);
view = Backrub._createBindView(attrName, this, context);
model_info = Backrub._resolveIsModel(attrName, this);
model_info.bind(function() {
if (execContext.isAlive()) {
view.rerender();
Expand All @@ -340,35 +340,35 @@
return new Handlebars.SafeString(view.render());
});
Handlebars.registerHelper("bindAttr", function(context) {
return _.bind(Template._bindAttr, this)(context.hash, context);
return _.bind(Backrub._bindAttr, this)(context.hash, context);
});
Handlebars.registerHelper("if", function(attr, context) {
return _.bind(Template._bindIf, this)(attr, context);
return _.bind(Backrub._bindIf, this)(attr, context);
});
Handlebars.registerHelper("unless", function(attr, context) {
var fn, inverse;
fn = context.fn;
inverse = context.inverse;
context.fn = inverse;
context.inverse = fn;
return _.bind(Template._bindIf, this)(attr, context);
return _.bind(Backrub._bindIf, this)(attr, context);
});
Handlebars.registerHelper("collection", function(attr, context) {
var colAtts, colTagName, colView, colViewPath, collection, execContext, itemAtts, itemTagName, itemView, itemViewPath, item_view, options, setup, view, views;
execContext = context.data.exec;
collection = Template._resolveValue(attr, this);
collection = Backrub._resolveValue(attr, this);
if (!(collection.each != null)) {
throw new Error("not a backbone collection!");
}
options = context.hash;
colViewPath = options != null ? options.colView : void 0;
if (colViewPath) {
colView = Template._getPath(colViewPath);
colView = Backrub._getPath(colViewPath);
}
colTagName = (options != null ? options.colTag : void 0) || "ul";
itemViewPath = options != null ? options.itemView : void 0;
if (itemViewPath) {
itemView = Template._getPath(itemViewPath);
itemView = Backrub._getPath(itemViewPath);
}
itemTagName = (options != null ? options.itemTag : void 0) || "li";
colAtts = {};
Expand All @@ -383,12 +383,12 @@
return itemAtts[k.substring(4).toLowerCase()] = v;
}
});
view = colView ? Template._createView(colView, {
view = colView ? Backrub._createView(colView, {
model: collection,
attributes: colAtts,
context: context,
tagName: (options != null ? options.colTag : void 0) ? colTagName : colView.prototype.tagName
}) : new Template._BindView({
}) : new Backrub._BindView({
tagName: colTagName,
attributes: colAtts,
attr: attr,
Expand All @@ -399,12 +399,12 @@
views = {};
item_view = function(m) {
var mview;
mview = itemView ? Template._createView(itemView, {
mview = itemView ? Backrub._createView(itemView, {
model: m,
attributes: itemAtts,
context: context,
tagName: (options != null ? options.itemTag : void 0) ? itemTagName : itemView.prototype.tagName
}) : new Template._BindView({
}) : new Backrub._BindView({
tagName: itemTagName,
attributes: itemAtts,
model: m,
Expand Down Expand Up @@ -460,4 +460,4 @@
});
return view.render();
});
}).call(this);
}).call(this);
4 changes: 2 additions & 2 deletions javascripts/todos.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ this.AppView = Backbone.View.extend({
});

$(function(){
var app = new Backbone.Template($("#app-template").text());
$("body").append(app.render());
var app = new Backbone.Backrub($("#app-template").html());
$("#app-template").after(app.render());
app.makeAlive();
});
Loading

0 comments on commit 0464387

Please sign in to comment.