silk.shop.resource = xb.core.object.extend( silk.postings.resource, { ctor: function( config ) { var handlers = { "categories": silk.shop.categories.mount, "view": silk.shop.items.mount, // "view-page": silk.postings.items.page.mount, // "view-page-sections": silk.postings.items.page.mount }; if ( typeof( this.handlers ) !== "object" ) { this.handlers = {}; } for ( var i in handlers ) { if ( typeof( this.handlers[ i ] ) === "undefined" ) { this.handlers[ i ] = handlers[ i ]; } } this.master = null; this.selection = {}; silk.resource.prototype.ctor.call( this, config ); }, init: function() { this.initEditorSettings(); this.master = silk.shop; this.master.init( this ); this.master.view(); }, getSelection: function() { return silk.shop.selection; }, getDataView: function( view, data ) { switch ( view ) { case "postings-items": return silk.data( data[ "postings-items" ], "posting-id" ); break; case "postings-categories": return silk.data( data[ "postings-categories" ], "category-id" ); break; } return silk.data.ptr( data[ "postings-items" ], data[ view ], "posting-id" ); }, initEditorSettings: function() { if ( typeof( editor ) !== "object" ) { return; } editor.loadToolbar( "/components/apps/postings/toolbar.postings.category.html" ); editor.loadToolbar( "/components/apps/postings/toolbar.postings.price.html" ); }, onRefresh: function( mount ) { console.warn( "refreshing", mount ); // this.mounts[ mount ].display(); }, view: function() { var __view = "list"; if ( typeof( this.data[ "postings-view" ] ) === "string" ) { __view = this.data[ "postings-view" ]; } document.body.setAttribute( "data-shop-view", __view ); var m = this.mounts[ "categories" ]; if ( m instanceof silk.node.mount ) { m.display( this.data[ m.fieldName ], true ); } var m = this.mounts[ "view" ]; if ( m instanceof silk.node.mount ) { m.display( this.data[ m.fieldName ], true ); } }, view_update: function() { var m = this.mounts[ "view" ]; if ( m instanceof silk.node.mount ) { m.display( this.data[ m.fieldName ], false ); } }, get: function( config, state, callback ) { var params = $.param( state ); this.loadURL( config.json, params, callback ); }, loadURL: function( url, params, callback ) { var params = ( ( typeof( params ) === "string" ) ? params : "" ); var http = new XMLHttpRequest(); http.open( "GET", url + "?" + params, true ); //http.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" ); http.onreadystatechange = function() { if ( http.readyState == 4 ) { if ( http.status == 200 ) { var result = null; try { result = JSON.parse( http.responseText ); } catch( e ) { console.error( "Could not parse", url, e ); }; if ( typeof( callback ) === "function" ) { return callback.call( null, result ); } return result; } } }; return http.send( params ); }, save: function() { return; var self = this; var data = {}; for ( var n in this.data ) { data[ n ] = this.data[ n ]; } for ( var n in this.mounts ) { data[ this.mounts[ n ].fieldName ] = this.mounts[ n ].save( this.data[ this.mounts[ n ].fieldName ] ); //.toJSON(); } for ( var n in data ) { if ( data[ n ] instanceof silk.data ) { data[ n ] = data[ n ].set; } } var params = JSON.stringify( data, null, "\t" ); //console.log( "saving string", params ); console.log( this.name, "saving data", data ); // this.putURL( silk.postings.get( this, "json" ), params, function( status, result ) { // console.log( "saved", status, result ); // } ); } } );