Skip to content

Commit

Permalink
wrapped in anonymous closure
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinavgujjar committed Aug 9, 2013
1 parent 6d8af2c commit 33fd5cf
Showing 1 changed file with 50 additions and 50 deletions.
100 changes: 50 additions & 50 deletions labs/dependency-examples/durandal/js/main.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,62 @@
/*global requirejs, define, ko */
'use strict';
(function () {
'use strict';

requirejs.config({
paths: {
'text': 'bower_components/durandal/amd/text'
},
baseUrl: './'
});
requirejs.config({
paths: {
'text': 'bower_components/durandal/amd/text'
},
baseUrl: './'
});

var ENTER_KEY = 13;
var ENTER_KEY = 13;

// a custom binding to handle the enter key (could go in a separate library)
ko.bindingHandlers.enterKey = {
init: function (element, valueAccessor, allBindingsAccessor, data) {
var wrappedHandler, newValueAccessor;
// a custom binding to handle the enter key (could go in a separate library)
ko.bindingHandlers.enterKey = {
init: function (element, valueAccessor, allBindingsAccessor, data) {
var wrappedHandler, newValueAccessor;

// wrap the handler with a check for the enter key
wrappedHandler = function (data, event) {
if (event.keyCode === ENTER_KEY) {
valueAccessor().call(this, data, event);
}
};

// create a valueAccessor with the options that we would want to pass to the event binding
newValueAccessor = function () {
return {
keyup: wrappedHandler
// wrap the handler with a check for the enter key
wrappedHandler = function (data, event) {
if (event.keyCode === ENTER_KEY) {
valueAccessor().call(this, data, event);
}
};
};

// call the real event binding's init function
ko.bindingHandlers.event.init(element, newValueAccessor, allBindingsAccessor, data);
}
};
// create a valueAccessor with the options that we would want to pass to the event binding
newValueAccessor = function () {
return {
keyup: wrappedHandler
};
};

// wrapper to hasfocus that also selects text and applies focus async
ko.bindingHandlers.selectAndFocus = {
init: function (element, valueAccessor, allBindingsAccessor) {
ko.bindingHandlers.hasfocus.init(element, valueAccessor, allBindingsAccessor);
ko.utils.registerEventHandler(element, 'focus', function () {
element.focus();
});
},
update: function (element, valueAccessor) {
ko.utils.unwrapObservable(valueAccessor()); // for dependency
// ensure that element is visible before trying to focus
setTimeout(function () {
ko.bindingHandlers.hasfocus.update(element, valueAccessor);
}, 0);
// call the real event binding's init function
ko.bindingHandlers.event.init(element, newValueAccessor, allBindingsAccessor, data);
}
};

};

// wrapper to hasfocus that also selects text and applies focus async
ko.bindingHandlers.selectAndFocus = {
init: function (element, valueAccessor, allBindingsAccessor) {
ko.bindingHandlers.hasfocus.init(element, valueAccessor, allBindingsAccessor);
ko.utils.registerEventHandler(element, 'focus', function () {
element.focus();
});
},
update: function (element, valueAccessor) {
ko.utils.unwrapObservable(valueAccessor()); // for dependency
// ensure that element is visible before trying to focus
setTimeout(function () {
ko.bindingHandlers.hasfocus.update(element, valueAccessor);
}, 0);
}
};

define(['bower_components/durandal/app',
'bower_components/durandal/viewLocator',
'bower_components/durandal/system',
'bower_components/durandal/plugins/router'],
function (app, viewLocator, system, router) {
define(['bower_components/durandal/app',
'bower_components/durandal/viewLocator',
'bower_components/durandal/system',
'bower_components/durandal/plugins/router'],
function (app, viewLocator, system, router) {

//>>excludeStart("build", true);
system.debug(true);
Expand All @@ -79,4 +78,5 @@ define(['bower_components/durandal/app',
//Show the app by setting the root view model for our application with a transition.
app.setRoot('js/viewmodels/shell');
});
});
});
}());

0 comments on commit 33fd5cf

Please sign in to comment.