From 852a3ca3ea3983bfbf1105322dfb658e24432fd5 Mon Sep 17 00:00:00 2001 From: Joscha Rohmann Date: Tue, 3 Nov 2015 09:47:28 +0100 Subject: [PATCH] Moved 'View._bindContext' to src/mv/bindContext.js. Replaced 'clonePrototype' in 'Application.extend' with 'bindContext'. --- src/mvc/Application.js | 7 ++++--- src/mvc/View.js | 22 ++++------------------ src/mvc/bindContext.js | 24 ++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 21 deletions(-) create mode 100644 src/mvc/bindContext.js diff --git a/src/mvc/Application.js b/src/mvc/Application.js index 061a91a..af9e6f2 100644 --- a/src/mvc/Application.js +++ b/src/mvc/Application.js @@ -6,8 +6,9 @@ define([ './Model', './Collection', './View', - './Property' -], function (blocks, clonePrototype, Router, History, Model, Collection, View, Property) { + './Property', + './bindContext' +], function (blocks, clonePrototype, Router, History, Model, Collection, View, Property, bindContext) { var application; blocks.Application = function (options) { @@ -269,7 +270,7 @@ define([ extend: function (obj) { blocks.extend(this, obj); - clonePrototype(obj, this); + bindContext(this, obj); return this; }, diff --git a/src/mvc/View.js b/src/mvc/View.js index aa2ecb6..5396858 100644 --- a/src/mvc/View.js +++ b/src/mvc/View.js @@ -1,15 +1,16 @@ define([ '../core', '../modules/ajax', - '../modules/Events' -], function (blocks, ajax, Events) { + '../modules/Events', + './bindContext' +], function (blocks, ajax, Events, bindContext) { /** * @namespace View */ function View(application, parentView) { var _this = this; - this._bindContext(); + bindContext(this); this._views = []; this._application = application; this._parentView = parentView || null; @@ -149,21 +150,6 @@ define([ this._application.navigateTo(view, params); }, - _bindContext: function () { - var key; - var value; - - for (key in this) { - value = this[key]; - - if (blocks.isObservable(value)) { - value.__context__ = this; - } else if (blocks.isFunction(value)) { - this[key] = blocks.bind(value, this); - } - } - }, - _tryInitialize: function (isActive) { if (!this._initialized && isActive) { if (this.options.url && !this._html) { diff --git a/src/mvc/bindContext.js b/src/mvc/bindContext.js new file mode 100644 index 0000000..d3e48ac --- /dev/null +++ b/src/mvc/bindContext.js @@ -0,0 +1,24 @@ +define([ + '../core' +], function (blocks) { + function bindContext (context, object) { + var key; + var value; + + if (!object) { + object = context; + } + + for (key in object) { + value = object[key]; + + if (blocks.isObservable(value)) { + context[key].__context__ = context; + } else if (blocks.isFunction(value)) { + context[key] = blocks.bind(value, context); + } + + } + } + return bindContext; +}); \ No newline at end of file