Skip to content

Instantly share code, notes, and snippets.

@danieleli
Created December 24, 2011 21:34
Show Gist options
  • Select an option

  • Save danieleli/1518404 to your computer and use it in GitHub Desktop.

Select an option

Save danieleli/1518404 to your computer and use it in GitHub Desktop.
Knockout Helpers
$(function() {
$.ajaxSetup({
cache: false
});
return window.CrudHelpers = {
ajaxAdd: function(url, dataToSave, callback) {
return this.ajaxModify(url, dataToSave, "POST", "add", callback);
},
ajaxUpdate: function(url, dataToSave, successCallback) {
dataToSave.ModifyDate = new Date();
return this.ajaxModify(url, dataToSave, "PUT", "update", successCallback);
},
ajaxDelete: function(url, dataToSave, successCallback) {
return this.ajaxModify(url + "/" + dataToSave, null, "DELETE", "deleted", successCallback);
},
ajaxModify: function(url, dataToSave, httpVerb, successMessage, callback) {
return $.ajax(url, {
data: dataToSave,
type: httpVerb,
dataType: 'json',
contentType: 'application/json',
success: function(data) {
return callback(data);
},
error: function(data) {
return humane("An error occured: " + data.responseText);
}
});
}
};
});
(function() {
$(function() {
return ko.bindingHandlers.executeOnEnter = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel) {
var value;
value = void 0;
value = valueAccessor();
return $(element).keypress(function(event) {
var keyCode;
keyCode = void 0;
keyCode = (event.which ? event.which : event.keyCode);
if (keyCode === 13) {
value.call(viewModel);
return false;
}
return true;
});
}
};
});
}).call(this);
//http://www.knockmeout.net/2011/03/guard-your-model-accept-or-cancel-edits.html
//wrapper to an observable that requires accept/cancel
$(function () {
ko.protectedObservable = function (initialValue) {
var result, _actualValue, _tempValue;
if (initialValue && typeof (initialValue) === "string" && initialValue.indexOf("/Date(") === 0) {
initialValue = new Date(parseInt(initialValue.match(/\d+/))).toString();
}
_actualValue = ko.observable(initialValue);
_tempValue = initialValue;
result = ko.dependentObservable({
read: function () {
return _actualValue();
},
write: function (newValue) {
return _tempValue = newValue;
}
});
result.commit = function () {
if (_tempValue !== _actualValue()) {
return _actualValue(_tempValue);
}
};
result.reset = function () {
_actualValue.valueHasMutated();
return _tempValue = _actualValue();
};
return result;
};
ko.protectedObservableItem = function (item) {
for (var param in item) {
if (item.hasOwnProperty(param)) {
this[param] = ko.protectedObservable(item[param]);
}
}
this.commit = function () {
for (var property in this) {
if (this.hasOwnProperty(property) && this[property].commit) this[property].commit();
}
}
};
ko.toProtectedObservableItemArray = function (sourceArray) {
var drillItems = ko.utils.arrayMap(sourceArray, function (item) {
return new ko.protectedObservableItem(item);
})
return drillItems;
};
ko.bindingHandlers.datepicker = {
init: function (element, valueAccessor, allBindingsAccessor) {
//initialize datepicker with some optional options
var options = allBindingsAccessor().datepickerOptions || {};
$(element).datepicker(options);
//handle the field changing
ko.utils.registerEventHandler(element, "change", function () {
var observable = valueAccessor();
observable($(element).datepicker("getDate"));
});
//handle disposal (if KO removes by the template binding)
ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
$(element).datepicker("destroy");
});
},
//update the control when the view model changes
update: function (element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
$(element).datepicker("setDate", value);
}
};
});
@praveensewak

Copy link
Copy Markdown

thanks for this bud. i have couple that you wanna grab and improve on - https://gist.github.com/praveensewak/5610427

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment