From d7c1576c835673f0859635eb5cad05c3bae0953d Mon Sep 17 00:00:00 2001 From: imrefazekas Date: Fri, 12 Jul 2013 23:19:00 +0200 Subject: [PATCH] toJSONByPrototype function added --- pager.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/pager.js b/pager.js index 68625f5..975a56e 100644 --- a/pager.js +++ b/pager.js @@ -56,6 +56,60 @@ delete copy['$__page__']; return copy; }; + viewModel.toJSONByPrototype = function(_theDataModel){ + var isNumber = function (obj) { + return (toString.call(obj) == "[object " + Number + "]") || !isNaN(obj); + }; + var isString = function (obj) { + return "[object String]" == toString.call(obj); + }; + var isObject = function (obj) { + return obj === Object(obj); + }; + var isBoolean = function(obj) { + return obj === true || obj === false || toString.call(obj) == '[object Boolean]'; + }; + var isArray = Array.isArray || function(obj) { + return toString.call(obj) == '[object Array]'; + }; + var isFunction = function (obj) { + return toString.call(obj) == "[object " + Function + "]"; + }; + if (typeof (/./) !== 'function') { + isFunction = function(obj) { + return typeof obj === 'function'; + }; + } + var toInnerJSON = function(obj, dataModel, viewModel){ + Object.keys(dataModel).forEach( function(key, index, array){ + var value = dataModel[key]; + if( isArray( value ) ){ + if( isFunction( viewModel[key] ) ){ + obj[ key ] = viewModel[key](); + } else if( isArray( viewModel[key] ) ){ + obj[ key ] = []; + viewModel[key].forEach( function(element, _index, _array){ + if( isFunction( element ) ) + obj[ key ].push( element() ); + } ); + } + } + else if( isString( value ) || isNumber( value ) || isBoolean( value ) ){ + if( viewModel[ key ] && isFunction( viewModel[ key ] ) ){ + obj[ key ] = viewModel[ key ](); + } + } + else if( isObject( value ) ){ + obj[ key ] = {}; + toInnerJSON( obj[ key ], dataModel[ key ], viewModel[ key ] ); + } + }); + return obj; + }; + + return toInnerJSON( {}, _theDataModel, this ); + }; + }; var fire = function (scope, name, options) {