Skip to content

Commit

Permalink
modified localStorage to handle models outside of collections, not ye…
Browse files Browse the repository at this point in the history
…t working
  • Loading branch information
llad committed May 17, 2011
1 parent e4f9752 commit a9f0e3c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 61 deletions.
73 changes: 24 additions & 49 deletions code.js
Expand Up @@ -17,29 +17,6 @@ $(function(){
//body: '',
},

// initize each plt with some defaults.
initialize: function() {
ds = this.defaults;
for (var k in ds) {
if (!this.get(k)) {
obj = {};
obj[k] = ds[k];
this.set(obj);
}
}

},



// // parse is called by Backbone before saving, so seems like a good time to add
// // in the mailto URL for the plt. However, it is also called before fetching
// // which we don't really need since it was saved. Might look at changing this.
// parse: function(response) {
// response.set({url: this.createURL(), silent: true});
// return response;
// },

// Remove this from *localStorage* and delete its view.
clear: function() {
this.destroy();
Expand All @@ -51,25 +28,19 @@ $(function(){
// options Model
// ----------
window.Options = Backbone.Model.extend({

localStorage: new Store("empltzOptions"),

// Default attributes for the template (plt).
defaults: {
sms: false
},

// initize each plt with some defaults.
initialize: function() {
ds = this.defaults;
for (var k in ds) {
if (!this.get(k)) {
obj = {};
obj[k] = ds[k];
this.set(obj);
}
}
}

});

// Create the user Options
window.userOptions = new Options();



// plt Collection
Expand Down Expand Up @@ -102,9 +73,7 @@ $(function(){

// Create our global collection of **pltz**.
window.Pltz = new PltList();

// Create the user Options
window.userOptions = new Options();


// Item View
// --------------
Expand Down Expand Up @@ -252,6 +221,8 @@ $(function(){
Pltz.bind('remove', this.updateList);

Pltz.fetch();
userOptions.fetch();
console.log(userOptions);

},

Expand Down Expand Up @@ -322,31 +293,35 @@ $(function(){
// start out making sure that submit is not bound from earlier calls.
// Need to do this because you can close the dialog box without submitting.
$('form#optionsForm').unbind('submit.options');
var opts = myOptions.toJSON();
opts = userOptions.toJSON();
console.log(userOptions);
console.log('isnew: ' + userOptions.isNew());
_.each(opts, function(v, k){
var smsSlider = $("select#slider");
if (k === 'sms') {
var smsSlider = $("select#slider");

if (v) {
smsSlider[0].selectedIndex = 1;
smsSlider.slider("refresh");
smsSlider[0].selectedIndex = 0;
}
else {
smsSlider[0].selectedIndex = 0;
smsSlider.slider("refresh");
smsSlider[0].selectedIndex = 1;
}
smsSlider.slider();
smsSlider.slider("refresh");
}
});

// TODO -- Left off here with options implementation.
$('form#optionForm').bind('submit.edit', function(e) {
$('form#optionsForm').bind('submit.options', function(e) {
var fields = $(this).serializeArray();
_.each(fields, function(field, i){
var name = field.name;
plt[name] = field.value;
userOptions.attributes[name] = (field.value === 'true');
});
thisPlt.save(plt);
console.log('isnew2: ' + userOptions.isNew());
userOptions.save();
console.log(userOptions);
$.mobile.changePage('list','pop',true);
$('this').unbind('submit.edit');
$('this').unbind('submit.options');
return false;
});
}
Expand Down
8 changes: 4 additions & 4 deletions index.html
Expand Up @@ -20,7 +20,7 @@

<div data-role="header">
<h1>empltz</h1>
<a id="optionButton" href="#options" data-rel="dialog" data-icon="gear" class="ui-btn-left" data-iconpos="notext">Options</a>
<a id="optionsButton" href="#options" data-rel="dialog" data-icon="gear" class="ui-btn-left" data-iconpos="notext">Options</a>
<a id="addButton" href="#add" data-rel="dialog" data-icon="plus" class="ui-btn-right" data-iconpos="notext">Add</a>
</div><!-- /header -->

Expand Down Expand Up @@ -113,9 +113,9 @@ <h1>Options</h1>
<form id="optionsForm" method="get" action="#" accept-charset="utf-8">
<div data-role="fieldcontain">
<label for="slider">SMS:</label>
<select name="slider" id="slider" data-role="slider">
<option value="off">Off</option>
<option value="on">On</option>
<select name="sms" id="slider" data-role="slider">
<option value="false">Off</option>
<option value="true">On</option>
</select>
<p> Turn on if your phone supports SMS URIs. iPhones don't.</p
</div>
Expand Down
55 changes: 47 additions & 8 deletions support/backbone-localstorage.js
Expand Up @@ -24,32 +24,55 @@ _.extend(Store.prototype, {

// Save the current state of the **Store** to *localStorage*.
save: function() {
console.log('save');
console.log(JSON.stringify(this.data));
localStorage.setItem(this.name, JSON.stringify(this.data));
},

// Add a model, giving it a (hopefully)-unique GUID, if it doesn't already
// have an id of it's own.
create: function(model) {
create: function(model,type) {
console.log('create');
if (!model.id) model.id = model.attributes.id = guid();
this.data[model.id] = model;
if (type === 'aModel') {
console.log('modelID ' + model.id);
console.log(this.data);
this.data = model.toJSON();
}
else {
this.data[model.id] = model;
}
this.save();
return model;
},

// Update a model by replacing its copy in `this.data`.
update: function(model) {
this.data[model.id] = model;
update: function(model, type) {
if (type === 'aModel') {
console.log('update');
console.log('model');
this.data = model;
}
else {
this.data[model.id] = model;
}
this.save();
return model;
},

// Retrieve a model from `this.data` by id.
find: function(model) {
find: function(model,type) {
console.log('find');
if (type === 'aModel') {
return this.data;
}
return this.data[model.id];

},

// Return the array of all models currently in storage.
findAll: function() {
console.log('findAll');
return _.values(this.data);
},

Expand All @@ -68,11 +91,27 @@ Backbone.sync = function(method, model, success, error) {

var resp;
var store = model.localStorage || model.collection.localStorage;
var type;
var modelID;

if (!model.collection) {
type = 'aModel';
modelID = model.id;
} else {
type = 'aCollection';
modelID = model.id;
}

console.log(method);
console.log(model);
console.log(model.id);



switch (method) {
case "read": resp = model.id ? store.find(model) : store.findAll(); break;
case "create": resp = store.create(model); break;
case "update": resp = store.update(model); break;
case "read": resp = model.id ? store.find(model,type) : store.findAll(); break;
case "create": resp = store.create(model,type); break;
case "update": resp = store.update(model,type); break;
case "delete": resp = store.destroy(model); break;
}

Expand Down

0 comments on commit a9f0e3c

Please sign in to comment.