Login Register

Dojo.data Issues

I am playing around with Dojo, and I wanted to be able to store start/end times in some sort of data store. However, storing the items has caused me some trouble. Below I have given relevant code..

var store = new dojo.data.ItemFileWriteStore({url: '/data/store.json'});
  
  // Passing in a random/unique id
  function startTime(id) {
    timers[id] = new Date();
			
    var storeTime = store.newItem({timerId: '1234', type: 'start', time: timers[id] });
    store.save({onComplete: saveDone, onError: saveFailed});

    var gotName = function(items, request) {
        for(var i = 0; i < items.length; i++) {
	    var item = items[i];
	    console.log("Located timer id that matched id:" + store.getLabel(item));
	}	
				
	if(items.length == 0) { console.log("No names were found"); }
    }; 
			
    var noName = function() { console.log("No names were found"); }
			
    var request = store.fetch({query: {timerId: id}, onComplete: gotName, onError: noName});
  }

  // other relevant functions like saveDone(), saveFailed()

My store.json file looks like this:

{ identifier: 'timerId',
  label: 'time',
  items: []
}

When I execute the startTime function, the store.save executes and returns a message for completion. I can then look up the entry using fetch(), and it returns a completion message and the data that was stored. However, it doesn't actually write anything to the external file.

Can anyone provide an explanation of how to write to a file? The dojo.data documentation regarding ItemFileWriteStore is missing information.

Much Thanks..

The ItemFileWriteStore

The ItemFileWriteStore doesn't save to any external file - you need to accommodate that using some sort of backend processing (php, jsp, asp, etc).

You can then implement a _saveEverything function in your store - it gets the following parameters: saveCompleteCallback, saveFailedCallback, newFileContentString.

You can then do something like this:

<div dojoType="dojo.data.ItemFileWriteStore" jsId="myStore">
  <script type="dojo/method" event="_saveEverything" args="saveCompleteCallback, saveFailedCallback, newFileContentString">
    // Do whatever is needed in order to post your data to your server...for example:
    dojo.xhrPost({content: {dataString: newFileContentString}, load: saveCompleteCallback, error: saveFailedCallback, url: "myUrlHandler.php"});
  </script>
</div>

The only thing to remember when you implement your _saveEverything function is you need to ensure that either saveCompleteCallback or saveFailedCallback is called when you are done.

This isn't because the ItemFileWriteStore is "incomplete" - because there are so many backends, and the backend needs to be the one in charge of actually saving the data, there isn't a way for the store to "know" about all possible combinations. Since it's fairly easy to implement and extend it, that is the best way to do it.