Skip to content

Instantly share code, notes, and snippets.

@jaredswarts55
Created March 27, 2014 16:35
Show Gist options
  • Select an option

  • Save jaredswarts55/9811856 to your computer and use it in GitHub Desktop.

Select an option

Save jaredswarts55/9811856 to your computer and use it in GitHub Desktop.
Automapping Models
/*
The MIT License (MIT)
Copyright (c) 2014, Jonathan Dexter, Jared Swarts
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
/**
* Creates a new AutoMapper object
* An AutoMapper is responsible for recursively filling in property information from the backend, into the ViewModel.
* Properties for AutoMapping are initialized by:
* prop = ko.observable(new classType())
* propArr = ko.observableArray([new classType()])
*
* @param {class} classType The type of class that is being mapped into
* @param {string} idColumn The name of the column that specifies the object's ID.
* @returns A new AutoMapper object
*/
(function ($, ko) {
/**
* Used to determine if the destination property of an object
* is an observable with a typeMapper.
*
* @param {object} property The property of the object (in itself an object) to check.
* @returns True if it is a non-array observable with a mappable type.
*/
function isAutoMappableObservableSingle(property) {
return ko.isObservable(property)
&& typeof property.typeMapper !== 'undefined'
&& typeof property.push === 'undefined';
};
/**
* Used to determine if the destination property of an object
* is an observable array with a typeMapper
*
* @param {object} property The property of the object (in itself an object) to check.
* @returns True if it is an observable array with a mappable type
*/
function isAutoMappableObservableArray(property) {
return ko.isObservable(property)
&& typeof ko.utils.unwrapObservable(property) === 'object'
&& typeof property.typeMapper !== 'undefined'
&& typeof property.push !== 'undefined';
};
ko.autoMapper = function (classType, idColumn) {
this.key = function (item) {
if (typeof item[idColumn] === 'function') {
return item[idColumn]();
} else {
return item[idColumn];
}
};
this.create = function (options) {
return new classType();
};
this.update = function (options) {
var self = this;
if (options.data === null) {
return options.target;
}
for (key in options.data) {
if (options.data.hasOwnProperty(key)) {
var val = options.data[key];
var property = options.target[key];
if (isAutoMappableObservableSingle(property)) {
var mappedItem = ko.mapping.fromJS(val, property.typeMapper, property);
return mappedItem;
}
else if (isAutoMappableObservableArray(property)) {
ko.utils.arrayForEach(val, function (item) {
var tempItem = ko.mapping.fromJS(item, property.typeMapper);
property().push(tempItem);
});
property.valueHasMutated();
}
else if (ko.isObservable(property)) {
property(val);
}
else {
property = val;
}
}
}
return options.target;
};
};
ko.extenders.typeMapper = function (target, mappingConfig) {
target.typeMapper = new ko.autoMapper(mappingConfig.Type, mappingConfig.Id || 'Id');
};
}(jQuery, ko));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment