Commit e67022dd authored by Ryan Eastridge's avatar Ryan Eastridge

update to latest thorax

parent 13e65b74
...@@ -52,7 +52,16 @@ _.extend(Thorax, { ...@@ -52,7 +52,16 @@ _.extend(Thorax, {
//view instances //view instances
_viewsIndexedByCid: {}, _viewsIndexedByCid: {},
templates: {}, templates: {},
Views: {} //view classes
Views: {},
//certain error prone pieces of code (on Android only it seems)
//are wrapped in a try catch block, then trigger this handler in
//the catch, with the name of the function or event that was
//trying to be executed. Override this with a custom handler
//to debug / log / etc
onException: function(name, err) {
throw err;
}
}); });
Thorax.Util = { Thorax.Util = {
...@@ -244,17 +253,8 @@ Thorax.View = Backbone.View.extend({ ...@@ -244,17 +253,8 @@ Thorax.View = Backbone.View.extend({
} }
//HelperView will not have mixins so need to check //HelperView will not have mixins so need to check
if (this.constructor.mixins) { this.constructor.mixins && _.each(this.constructor.mixins, applyMixin, this);
//mixins this.mixins && _.each(this.mixins, applyMixin, this);
for (var i = 0; i < this.constructor.mixins.length; ++i) {
applyMixin.call(this, this.constructor.mixins[i]);
}
if (this.mixins) {
for (var i = 0; i < this.mixins.length; ++i) {
applyMixin.call(this, this.mixins[i]);
}
}
}
//_events not present on HelperView //_events not present on HelperView
this.constructor._events && this.constructor._events.forEach(function(event) { this.constructor._events && this.constructor._events.forEach(function(event) {
...@@ -328,10 +328,14 @@ Thorax.View = Backbone.View.extend({ ...@@ -328,10 +328,14 @@ Thorax.View = Backbone.View.extend({
}, },
_getContext: function(attributes) { _getContext: function(attributes) {
return _.extend({}, Thorax.Util.getValue(this, 'context'), attributes || {}, { var data = _.extend({}, Thorax.Util.getValue(this, 'context'), attributes || {}, {
cid: _.uniqueId('t'), cid: _.uniqueId('t'),
yield: function() {
return data.fn && data.fn(data);
},
_view: this _view: this
}); });
return data;
}, },
renderTemplate: function(file, data, ignoreErrors) { renderTemplate: function(file, data, ignoreErrors) {
...@@ -410,7 +414,7 @@ Handlebars.registerHelper('super', function() { ...@@ -410,7 +414,7 @@ Handlebars.registerHelper('super', function() {
}); });
Handlebars.registerHelper('template', function(name, options) { Handlebars.registerHelper('template', function(name, options) {
var context = _.extend({}, this, options ? options.hash : {}); var context = _.extend({fn: options && options.fn}, this, options ? options.hash : {});
var output = Thorax.View.prototype.renderTemplate.call(this._view, name, context); var output = Thorax.View.prototype.renderTemplate.call(this._view, name, context);
return new Handlebars.SafeString(output); return new Handlebars.SafeString(output);
}); });
...@@ -574,8 +578,8 @@ function applyMixin(mixin) { ...@@ -574,8 +578,8 @@ function applyMixin(mixin) {
} }
var _destroy = Thorax.View.prototype.destroy, var _destroy = Thorax.View.prototype.destroy,
_on = Thorax.View.prototype.on, _on = Thorax.View.prototype.on,
_delegateEvents = Thorax.View.prototype.delegateEvents; _delegateEvents = Thorax.View.prototype.delegateEvents;
...@@ -615,6 +619,9 @@ _.extend(Thorax.View, { ...@@ -615,6 +619,9 @@ _.extend(Thorax.View, {
_.extend(Thorax.View.prototype, { _.extend(Thorax.View.prototype, {
freeze: function(options) { freeze: function(options) {
this.model && this._unbindModelEvents();
options = _.defaults(options || {}, { options = _.defaults(options || {}, {
dom: true, dom: true,
children: true children: true
...@@ -703,10 +710,10 @@ _.extend(Thorax.View.prototype, { ...@@ -703,10 +710,10 @@ _.extend(Thorax.View.prototype, {
_addEvent: function(params) { _addEvent: function(params) {
if (params.type === 'view') { if (params.type === 'view') {
params.name.split(/\s+/).forEach(function(name) { params.name.split(/\s+/).forEach(function(name) {
_on.call(this, name, params.handler, params.context || this); _on.call(this, name, bindEventHandler.call(this, 'view-event:' + params.name, params.handler), params.context || this);
}, this); }, this);
} else { } else {
var boundHandler = containHandlerToCurentView(bindEventHandler.call(this, params.handler), this.cid); var boundHandler = containHandlerToCurentView(bindEventHandler.call(this, 'dom-event:' + params.name, params.handler), this.cid);
if (params.selector) { if (params.selector) {
//TODO: determine why collection views and some nested views //TODO: determine why collection views and some nested views
//need defered event delegation //need defered event delegation
...@@ -747,12 +754,18 @@ function containHandlerToCurentView(handler, cid) { ...@@ -747,12 +754,18 @@ function containHandlerToCurentView(handler, cid) {
} }
} }
function bindEventHandler(callback) { function bindEventHandler(eventName, callback) {
var method = typeof callback === 'function' ? callback : this[callback]; var method = typeof callback === 'function' ? callback : this[callback];
if (!method) { if (!method) {
throw new Error('Event "' + callback + '" does not exist'); throw new Error('Event "' + callback + '" does not exist ' + (this.name || this.cid) + ':' + eventName);
} }
return _.bind(method, this); return _.bind(function() {
try {
method.apply(this, arguments);
} catch (e) {
Thorax.onException('thorax-exception: ' + (this.name || this.cid) + ':' + eventName, e);
}
}, this);
} }
function eventParamsFromEventItem(name, handler, context) { function eventParamsFromEventItem(name, handler, context) {
...@@ -774,9 +787,7 @@ function eventParamsFromEventItem(name, handler, context) { ...@@ -774,9 +787,7 @@ function eventParamsFromEventItem(name, handler, context) {
} }
var modelCidAttributeName = 'data-model-cid', var modelCidAttributeName = 'data-model-cid',
modelNameAttributeName = 'data-model-name', modelNameAttributeName = 'data-model-name';
_freeze = Thorax.View.prototype.freeze,
_context = Thorax.View.prototype.context;
Thorax.Model = Backbone.Model.extend({ Thorax.Model = Backbone.Model.extend({
isEmpty: function() { isEmpty: function() {
...@@ -808,6 +819,8 @@ Thorax.Util.createRegistryWrapper(Thorax.Model, Thorax.Models); ...@@ -808,6 +819,8 @@ Thorax.Util.createRegistryWrapper(Thorax.Model, Thorax.Models);
Thorax.View._modelEvents = []; Thorax.View._modelEvents = [];
function addEvents(target, source) { function addEvents(target, source) {
...@@ -823,18 +836,8 @@ function addEvents(target, source) { ...@@ -823,18 +836,8 @@ function addEvents(target, source) {
} }
_.extend(Thorax.View.prototype, { _.extend(Thorax.View.prototype, {
_getContext: function(attributes) {
return _.extend({}, Thorax.Util.getValue(this, 'context', this.model), attributes || {}, {
cid: _.uniqueId('t'),
_view: this
});
},
context: function() { context: function() {
return _.extend({}, _context.call(this), (this.model && this.model.attributes) || {}); return _.extend({}, this, (this.model && this.model.attributes) || {});
},
freeze: function(options) {
this.model && this._unbindModelEvents();
_freeze.call(this, options);
}, },
_bindModelEvents: function() { _bindModelEvents: function() {
bindModelEvents.call(this, this.constructor._modelEvents); bindModelEvents.call(this, this.constructor._modelEvents);
...@@ -1841,8 +1844,12 @@ Thorax.loadHandler = function(start, end) { ...@@ -1841,8 +1844,12 @@ Thorax.loadHandler = function(start, end) {
function startLoadTimeout() { function startLoadTimeout() {
clearTimeout(self._loadStart.timeout); clearTimeout(self._loadStart.timeout);
self._loadStart.timeout = setTimeout(function() { self._loadStart.timeout = setTimeout(function() {
self._loadStart.run = true; try {
start.call(self, self._loadStart.message, self._loadStart.background, self._loadStart); self._loadStart.run = true;
start.call(self, self._loadStart.message, self._loadStart.background, self._loadStart);
} catch(e) {
Thorax.onException('loadStart', e);
}
}, },
loadingTimeout*1000); loadingTimeout*1000);
} }
...@@ -1887,19 +1894,23 @@ Thorax.loadHandler = function(start, end) { ...@@ -1887,19 +1894,23 @@ Thorax.loadHandler = function(start, end) {
events.splice(index, 1); events.splice(index, 1);
} }
if (!events.length) { if (!events.length) {
self._loadStart.endTimeout = setTimeout(function(){ self._loadStart.endTimeout = setTimeout(function() {
if (!events.length) { try {
var run = self._loadStart.run; if (!events.length) {
var run = self._loadStart.run;
if (run) {
// Emit the end behavior, but only if there is a paired start if (run) {
end.call(self, self._loadStart.background, self._loadStart); // Emit the end behavior, but only if there is a paired start
self._loadStart.trigger(loadEnd, self._loadStart); end.call(self, self._loadStart.background, self._loadStart);
self._loadStart.trigger(loadEnd, self._loadStart);
}
// If stopping make sure we don't run a start
clearTimeout(self._loadStart.timeout);
self._loadStart = undefined;
} }
} catch(e) {
// If stopping make sure we don't run a start Thorax.onException('loadEnd', e);
clearTimeout(self._loadStart.timeout);
self._loadStart = undefined;
} }
}, loadingEndTimeout * 1000); }, loadingEndTimeout * 1000);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment