Opened 9 years ago

Closed 7 years ago

#11382 closed enhancement (invalid)

[patch][cla] Data Store to interact with CakePHP via JSON

Reported by: mastepanoski Owned by: mastepanoski
Priority: high Milestone: 2.0
Component: DojoX Data Version: 1.4.3
Keywords: data store dojox cakephp json rest Cc:
Blocked By: Blocking:

Description

The aim is made easy to pull data from your database through JSON and REST-like services from CakePHP through CakeRestStore?:

dojo.provide("dojox.data.CakeRestStore");

dojo.require("dojox.data.JsonRestStore");

dojo.declare("dojox.data.CakeRestStore", dojox.data.JsonRestStore, {
	"-chains-" : {
	       constructor : "manual"
	},
	cakeObjectToQuery : function(/* Object */map) {
		var enc = encodeURIComponent;
		var pairs = [];
		var value = '';
		var assign = '';
		for ( var name in map) {
			value = map[name];
			assign = name + ":";
			if (dojo.isArray(value)) {
	  		    for ( var i = 0; i < value.length; i++) 
                            {
				if (value[i] && isNaN(value[i])) {
					value[i] = value[i].replace('/', '');
				}				
				pairs.push(assign + enc(value[i]));
			    }
			} else {
			    if (value && isNaN(value)) {
				value = value.replace('/', '');
			    }
 			    pairs.push(assign + enc(value));
			}
		}
		return pairs.join("/");
	},
	constructor : function(options) {
		if (typeof options.target == 'string' && !options.service) {
			// if exist quit last '/' and json extension.
			options.target = options.target.replace(/\.json$/g, '').replace(/\/$/g, '');
		}
		this.inherited(arguments);
	},
	fetch : function(args) {
		args = args || {};
		var self = this;
		function addToQueryStr(obj) {
			function buildInitialQueryString() {
				if (args.queryStr == null) {
					args.queryStr = '';
				}
				if (dojo.isObject(args.query)) {
					args.queryStr = self.cakeObjectToQuery(args.query);
				} else if (dojo.isString(args.query)) {
				        args.queryStr = encodeURI(args.query.replace('/', ''));
			    }
			}
			if (args.queryStr == null) {
				buildInitialQueryString();
			}
			args.queryStr = args.queryStr.replace(/\.json$/g,'') + '/' + self.cakeObjectToQuery(obj) + '.json';
		}
		if (args.start || args.count) {
		        // each page must start with a row number multiple
			// of count.
		        if ((args.start || 0) % args.count) {
			     throw new Error("The start parameter must be a multiple of the count parameter");
			}
			addToQueryStr( {page : ((args.start || 0) / args.count) + 1, show : args.count});
		}
		if (args.sort) {
			var queryObj = {
				sort : [],
				direction : []
			};
			dojo.forEach(args.sort,	function(item) {
			   queryObj.sort.push(item.attribute);               queryObj.direction.push(item.descending ? 'desc' : 'asc');
									        });
		addToQueryStr(queryObj);
		delete args.sort;
	   }
  	   if (args.queryStr[0] === '/') {
		args.queryStr = args.queryStr.substr(1);
	   }
  	   return this.inherited(arguments);
         },
	_processResults : function(results, deferred) {
		if (results['items']) {
			var items = results['items'];
			var count = 0;
		        if (results['numRows']) {
			    count = results['numRows'];
			} else {
			    count = items.length;
			}
			return {
			   totalCount : deferred.fullLength || (deferred.request.count == count ? (deferred.request.start || 0) + count * 2 : count),
			   items : items
			};
		}
		return this.inherited(arguments);
	 }
});

I have been using a class like this on several Web projects.

Attachments (1)

CakeRestStore.js (3.3 KB) - added by mastepanoski 9 years ago.
CakeRestStore? implementation

Download all attachments as: .zip

Change History (11)

Changed 9 years ago by mastepanoski

Attachment: CakeRestStore.js added

CakeRestStore? implementation

comment:1 Changed 9 years ago by Jared Jurkiewicz

Have you signed a CLA?

No code can be accepted without a CLA on file.

comment:2 Changed 9 years ago by mastepanoski

Yes, I have.

"Dear Mauro Alberto Stepanoski:

Welcome to the Dojo Foundation open source community!

This email confirms that your Contributor License Agreement has been received and your name has been added to the Dojo Foundation's contributor listing. Our committers are now authorized to accept your contributions to Dojo Foundation projects which currently include the Dojo Toolkit, DWR, OpenRecord?, Cometd, Persevere, Lucid Desktop, Sizzle, & General Interface.

On behalf of the Dojo Foundation and its supporting communities, I want to sincerely thank you for sharing your work and knowledge in our continued efforts to keep the web open.

Thank you!

-- Aimee Evans The Dojo Foundation 800 West El Camino Real Suite 180 Mountain View, CA 94040 Fax: 650-618-0300 http://dojotoolkit.org"

comment:3 in reply to:  1 Changed 9 years ago by mastepanoski

Replying to jaredj:

Have you signed a CLA?

No code can be accepted without a CLA on file.

Yes, I have. I received an email from Dojo Foundation. This email confirms that my CLA has been received and my name (Mauro Stepanoski) has been added to the Dojo Foundation's contributor listing.

comment:4 Changed 9 years ago by Jared Jurkiewicz

One more question:

Are you willing to document the store in the dojo wiki and fix bugs in the data store if they come in? I'm wary to commit something if there isn't someone backing it.

comment:5 in reply to:  4 Changed 9 years ago by mastepanoski

Replying to jaredj:

One more question:

Are you willing to document the store in the dojo wiki and fix bugs in the data store if they come in? I'm wary to commit something if there isn't someone backing it.

Yes, I'm willing to document and fix bugs.

comment:6 Changed 9 years ago by Jared Jurkiewicz

Milestone: tbd1.7

comment:7 Changed 8 years ago by Chris Mitchell

Milestone: 1.71.8

comment:8 Changed 7 years ago by Colin Snover

Milestone: 1.82.0

1.8 is frozen. Move all enhancements to next release. If you need an exemption from the freeze for this ticket, contact me immediately.

comment:9 Changed 7 years ago by dylan

Owner: changed from Jared Jurkiewicz to mastepanoski
Status: newpending
Summary: Data Store to interact with CakePHP via JSON[patch][cla] Data Store to interact with CakePHP via JSON

We did not do enough to get this into Dojo years ago.

At this time, dojo/data is deprecated in favor of dojo/store.

I would recommend doing something like https://github.com/kfranqueiro/dojo-smore for this, if you're still interested in having this type of integration between CakePHP and dojo/store.

If that's of interest, please reply, otherwise this will be closed in a couple of weeks.

comment:10 Changed 7 years ago by trac-o-bot

Resolution: invalid
Status: pendingclosed

Because we get so many tickets, we often need to return them to the initial reporter for more information. If that person does not reply within 14 days, the ticket will automatically be closed, and that has happened in this case. If you still are interested in pursuing this issue, feel free to add a comment with the requested information and we will be happy to reopen the ticket if it is still valid. Thanks!

Note: See TracTickets for help on using tickets.