diff --git a/code.js b/code.js index 6b22800..ac9693b 100644 --- a/code.js +++ b/code.js @@ -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(); @@ -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 @@ -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 // -------------- @@ -252,6 +221,8 @@ $(function(){ Pltz.bind('remove', this.updateList); Pltz.fetch(); + userOptions.fetch(); + console.log(userOptions); }, @@ -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; }); } diff --git a/index.html b/index.html index 047abda..7b0ed68 100644 --- a/index.html +++ b/index.html @@ -20,7 +20,7 @@

empltz

- Options + Options Add
@@ -113,9 +113,9 @@

Options

- + +

Turn on if your phone supports SMS URIs. iPhones don't.

diff --git a/support/backbone-localstorage.js b/support/backbone-localstorage.js index 185c822..582a294 100644 --- a/support/backbone-localstorage.js +++ b/support/backbone-localstorage.js @@ -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); }, @@ -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; }