Skip to content

Commit

Permalink
allow afterRender on module binding to be defined later when passing …
Browse files Browse the repository at this point in the history
…an observable and initial value is null/undefined.

fixes #16
  • Loading branch information
rniemeyer committed Mar 17, 2014
1 parent ae43190 commit 2846ca7
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 25 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
20 changes: 9 additions & 11 deletions build/knockout-amd-helpers.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions build/knockout-amd-helpers.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
16 changes: 16 additions & 0 deletions spec/amdBindings.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
18 changes: 8 additions & 10 deletions src/amdBindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit 2846ca7

Please sign in to comment.