diff --git a/bower.json b/bower.json index 30ed46d..44144c6 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "knockout-amd-helpers", - "version": "0.6.1", + "version": "0.6.2", "main": "build/knockout-amd-helpers.min.js", "ignore": [ "examples", diff --git a/build/knockout-amd-helpers.js b/build/knockout-amd-helpers.js index 0976404..1aed107 100644 --- a/build/knockout-amd-helpers.js +++ b/build/knockout-amd-helpers.js @@ -1,4 +1,4 @@ -// knockout-amd-helpers 0.6.1 | (c) 2014 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license +// knockout-amd-helpers 0.6.2 | (c) 2014 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license define(["knockout"], function(ko) { //helper functions to support the binding and template engine (whole lib is wrapped in an IIFE) @@ -45,18 +45,16 @@ ko.bindingHandlers.module = { disposeMethod = ko.bindingHandlers.module.disposeMethod; //build up a proper template binding object - if (options && typeof options === "object") { - templateBinding.templateEngine = options.templateEngine; + templateBinding.templateEngine = options && options.templateEngine; - //afterRender could be different for each module, create a wrapper - templateBinding.afterRender = function() { - var options = unwrap(valueAccessor()); + //afterRender could be different for each module, create a wrapper + templateBinding.afterRender = function() { + var options = unwrap(valueAccessor()); - if (options && typeof options.afterRender === "function") { - options.afterRender.apply(this, arguments); - } - }; - } + if (options && typeof options.afterRender === "function") { + options.afterRender.apply(this, arguments); + } + }; //if this is not an anonymous template, then build a function to properly return the template name if (!isAnonymous(element)) { diff --git a/build/knockout-amd-helpers.min.js b/build/knockout-amd-helpers.min.js index ac20cde..030f373 100644 --- a/build/knockout-amd-helpers.min.js +++ b/build/knockout-amd-helpers.min.js @@ -1,2 +1,2 @@ -// knockout-amd-helpers 0.6.1 | (c) 2014 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license -define(["knockout"],function(a){var b=window.require||window.curl,c=a.utils.unwrapObservable,d=function(a,b){var c,d=function(){return a.apply(this,b||[])};return d.prototype=a.prototype,c=new d,c.constructor=a,c},e=function(a){return a&&a.replace(/\/?$/,"/")},f=function(b){for(var c=a.virtualElements.firstChild(b);c;){if(1===c.nodeType||8===c.nodeType)return!0;c=a.virtualElements.nextSibling(c)}return!1};a.bindingHandlers.module={init:function(g,h,i,j,k){var l,m,n=h(),o=c(n),p={},q=a.bindingHandlers.module.initializer,r=a.bindingHandlers.module.disposeMethod;return o&&"object"==typeof o&&(p.templateEngine=o.templateEngine,p.afterRender=function(){var a=c(h());a&&"function"==typeof a.afterRender&&a.afterRender.apply(this,arguments)}),f(g)||(p.name=function(){var a=c(n);return(a&&"object"==typeof a?c(a.template||a.name):a)||""}),p.data=a.observable(),p["if"]=p.data,a.applyBindingsToNode(g,{template:p},l=k.extend({$module:null})),m=function(){a.computed(function(){var a=p.data();a&&("function"==typeof a[r]&&(a[r].call(a),a=null),p.data(null))}).dispose()},a.computed({read:function(){var f,g=c(n);g&&"object"==typeof g&&(q=g.initializer||q,r=g.disposeMethod||r,f=[].concat(c(g.data)),g=c(g.name)),m(),g&&b([e(a.bindingHandlers.module.baseDir)+g],function(a){"function"==typeof a?a=d(a,f):a&&a[q]&&(a=a[q].apply(a,f||[])||a),l.$module=a,p.data(a)})},disposeWhenNodeIsRemoved:g}),a.utils.domNodeDisposal.addDisposeCallback(g,m),{controlsDescendantBindings:!0}},baseDir:"",initializer:"initialize",disposeMethod:"dispose"},a.virtualElements&&(a.virtualElements.allowedBindings.module=!0),function(a,b){var c=new a.nativeTemplateEngine,d=(c.renderTemplate,{});c.defaultPath="templates",c.defaultSuffix=".tmpl.html",c.defaultRequireTextPluginName="text",a.templateSources.requireTemplate=function(b){this.key=b,this.template=a.observable(" "),this.requested=!1,this.retrieved=!1},a.templateSources.requireTemplate.prototype.text=function(){return!this.requested&&this.key&&(b([c.defaultRequireTextPluginName+"!"+e(c.defaultPath)+this.key+c.defaultSuffix],function(a){this.retrieved=!0,this.template(a)}.bind(this)),this.requested=!0),this.key||this.template(""),0===arguments.length?this.template():void 0},c.makeTemplateSource=function(b,c){var e;return"string"==typeof b?(e=(c||document).getElementById(b),e&&"script"===e.tagName.toLowerCase()?new a.templateSources.domElement(e):(b in d||(d[b]=new a.templateSources.requireTemplate(b)),d[b])):!b||1!==b.nodeType&&8!==b.nodeType?void 0:new a.templateSources.anonymousTemplate(b)},c.renderTemplate=function(b,d,e,f){var g=c.makeTemplateSource(b,f),h=e&&e.afterRender;return"function"==typeof h&&g instanceof a.templateSources.requireTemplate&&!g.retrieved&&(e.afterRender=function(){g.retrieved&&h.apply(this,arguments)}),c.renderTemplateSource(g,d,e)},a.amdTemplateEngine=c,a.setTemplateEngine(c)}(a,b)}); \ No newline at end of file +// knockout-amd-helpers 0.6.2 | (c) 2014 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license +define(["knockout"],function(a){var b=window.require||window.curl,c=a.utils.unwrapObservable,d=function(a,b){var c,d=function(){return a.apply(this,b||[])};return d.prototype=a.prototype,c=new d,c.constructor=a,c},e=function(a){return a&&a.replace(/\/?$/,"/")},f=function(b){for(var c=a.virtualElements.firstChild(b);c;){if(1===c.nodeType||8===c.nodeType)return!0;c=a.virtualElements.nextSibling(c)}return!1};a.bindingHandlers.module={init:function(g,h,i,j,k){var l,m,n=h(),o=c(n),p={},q=a.bindingHandlers.module.initializer,r=a.bindingHandlers.module.disposeMethod;return p.templateEngine=o&&o.templateEngine,p.afterRender=function(){var a=c(h());a&&"function"==typeof a.afterRender&&a.afterRender.apply(this,arguments)},f(g)||(p.name=function(){var a=c(n);return(a&&"object"==typeof a?c(a.template||a.name):a)||""}),p.data=a.observable(),p["if"]=p.data,a.applyBindingsToNode(g,{template:p},l=k.extend({$module:null})),m=function(){a.computed(function(){var a=p.data();a&&("function"==typeof a[r]&&(a[r].call(a),a=null),p.data(null))}).dispose()},a.computed({read:function(){var f,g=c(n);g&&"object"==typeof g&&(q=g.initializer||q,r=g.disposeMethod||r,f=[].concat(c(g.data)),g=c(g.name)),m(),g&&b([e(a.bindingHandlers.module.baseDir)+g],function(a){"function"==typeof a?a=d(a,f):a&&a[q]&&(a=a[q].apply(a,f||[])||a),l.$module=a,p.data(a)})},disposeWhenNodeIsRemoved:g}),a.utils.domNodeDisposal.addDisposeCallback(g,m),{controlsDescendantBindings:!0}},baseDir:"",initializer:"initialize",disposeMethod:"dispose"},a.virtualElements&&(a.virtualElements.allowedBindings.module=!0),function(a,b){var c=new a.nativeTemplateEngine,d=(c.renderTemplate,{});c.defaultPath="templates",c.defaultSuffix=".tmpl.html",c.defaultRequireTextPluginName="text",a.templateSources.requireTemplate=function(b){this.key=b,this.template=a.observable(" "),this.requested=!1,this.retrieved=!1},a.templateSources.requireTemplate.prototype.text=function(){return!this.requested&&this.key&&(b([c.defaultRequireTextPluginName+"!"+e(c.defaultPath)+this.key+c.defaultSuffix],function(a){this.retrieved=!0,this.template(a)}.bind(this)),this.requested=!0),this.key||this.template(""),0===arguments.length?this.template():void 0},c.makeTemplateSource=function(b,c){var e;return"string"==typeof b?(e=(c||document).getElementById(b),e&&"script"===e.tagName.toLowerCase()?new a.templateSources.domElement(e):(b in d||(d[b]=new a.templateSources.requireTemplate(b)),d[b])):!b||1!==b.nodeType&&8!==b.nodeType?void 0:new a.templateSources.anonymousTemplate(b)},c.renderTemplate=function(b,d,e,f){var g=c.makeTemplateSource(b,f),h=e&&e.afterRender;return"function"==typeof h&&g instanceof a.templateSources.requireTemplate&&!g.retrieved&&(e.afterRender=function(){g.retrieved&&h.apply(this,arguments)}),c.renderTemplateSource(g,d,e)},a.amdTemplateEngine=c,a.setTemplateEngine(c)}(a,b)}); \ No newline at end of file diff --git a/package.json b/package.json index a1e2c32..5ed7a8d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "knockout-amd-helpers", - "version": "0.6.1", + "version": "0.6.2", "devDependencies": { "grunt": "~0.4.1", "grunt-contrib-uglify": "~0.2.0", diff --git a/spec/amdBindings.spec.js b/spec/amdBindings.spec.js index 9927175..7864593 100644 --- a/spec/amdBindings.spec.js +++ b/spec/amdBindings.spec.js @@ -469,6 +469,22 @@ define(["knockout", "knockout-amd-helpers"], function(ko) { }); }); + it("should call afterRender, even when initial value was null", function(done) { + var observable = ko.observable(null), + afterRender = jasmine.createSpy("afterRender"); + + applyBindings("module: test", { test: observable }, null, function() { + updateObservable(observable, { + name: "static-no-initialize", + afterRender: afterRender + }, function() { + expect(container.innerText).toEqual("static-no-initialize: Bob Smith"); + expect(afterRender.calls.count()).toEqual(1); + done(); + }); + }); + }); + describe("observable is empty", function() { it("should not error out initially", function(done) { var observable = ko.observable(); diff --git a/src/amdBindings.js b/src/amdBindings.js index 62f8287..d2d07c0 100644 --- a/src/amdBindings.js +++ b/src/amdBindings.js @@ -9,18 +9,16 @@ ko.bindingHandlers.module = { disposeMethod = ko.bindingHandlers.module.disposeMethod; //build up a proper template binding object - if (options && typeof options === "object") { - templateBinding.templateEngine = options.templateEngine; + templateBinding.templateEngine = options && options.templateEngine; - //afterRender could be different for each module, create a wrapper - templateBinding.afterRender = function() { - var options = unwrap(valueAccessor()); + //afterRender could be different for each module, create a wrapper + templateBinding.afterRender = function() { + var options = unwrap(valueAccessor()); - if (options && typeof options.afterRender === "function") { - options.afterRender.apply(this, arguments); - } - }; - } + if (options && typeof options.afterRender === "function") { + options.afterRender.apply(this, arguments); + } + }; //if this is not an anonymous template, then build a function to properly return the template name if (!isAnonymous(element)) {