Skip to content
Browse files

add dispose() to Model

fix sync.Local
  • Loading branch information...
1 parent e70f831 commit d0c98565347190677bcb1a98b2446870262a5da7 @rhysbrettbowen committed
Showing with 57 additions and 21 deletions.
  1. +5 −0 README.md
  2. +20 −6 model.js
  3. +3 −4 sync/local.js
  4. +2 −1 testApp/testApp.html
  5. +23 −6 testApp/testApp.js
  6. +4 −4 test_deps.js
View
5 README.md
@@ -64,6 +64,11 @@ mvc.Router uses goog.History and hash tokens to hold and manage the state of the
### changelog ###
+#### v0.3 ####
+
+- add dispose() to mvc.Model
+- fix mvc.sync.Local
+
#### v0.2 ####
- fix model.Schema
View
26 model.js
@@ -1,4 +1,4 @@
-// v0.2
+// v0.3
goog.provide('mvc.Model');
goog.provide('mvc.model.Schema');
@@ -21,7 +21,7 @@ goog.require('goog.object');
* @param {Object} attr
* @param {mvc.Model.Schema=} schema
*/
-mvc.Model = function(attr, schema) {
+mvc.Model = function(attr, schema, sync) {
/**
* @private
* @type {Object.<string, ?Object>}
@@ -38,11 +38,15 @@ mvc.Model = function(attr, schema) {
*/
this.schema_ = schema;
- this.cid_ = goog.getUid();
+ this.sync_ = sync;
+
+ this.cid_ = goog.getUid(this);
goog.object.forEach(attr, function(val, name) {
this.attr_[name] = val;
}, this);
+
+ this.dispatchEvent(goog.events.EventType.LOAD);
};
goog.inherits(mvc.Model, goog.events.EventTarget);
@@ -56,6 +60,10 @@ mvc.Model.prototype.toJson = function() {
return goog.object.clone(this.attr_);
};
+mvc.Model.prototype.setSync = function(sync) {
+ this.sync_ = sync;
+}
+
/**
* @param {string} key
* @return {Object=}
@@ -68,7 +76,7 @@ mvc.Model.prototype.get = function(key) {
* @return {boolean}
*/
mvc.Model.prototype.isNew = function() {
- return !!this.get('id');
+ return !this.get('id');
}
/**
@@ -127,7 +135,7 @@ mvc.Model.prototype.unset = function(key, silent) {
* fires the change event for the model
*/
mvc.Model.prototype.change = function() {
- this.dispatchEvent(goog.events.EventType.change);
+ this.dispatchEvent(goog.events.EventType.CHANGE);
};
/**
@@ -166,6 +174,12 @@ mvc.Model.prototype.revert = function() {
return this;
};
+mvc.Model.prototype.dispose = function() {
+ this.sync_.delete(this);
+ this.dispatchEvent(goog.events.EventType.UNLOAD);
+ this.disposeInternal();
+}
+
/**
* reads an object fomr an external source using sync
*
@@ -188,7 +202,7 @@ mvc.Model.prototype.fetch = function(callback, silent) {
callback(data, status, this);
}
}, this);
- this.sync.read(this);
+ this.sync_.read(this);
};
/**
View
7 sync/local.js
@@ -1,4 +1,4 @@
-// v0.2
+// v0.3
goog.provide('mvc.sync.Local');
goog.require('goog.storage.Storage');
@@ -8,7 +8,7 @@ goog.require('goog.storage.mechanism.HTML5LocalStorage');
* @implements {mvc.sync}
*/
mvc.sync.Local = function() {
- this.store_ = new goog.storage.Storage(goog.storage.mechanism.HTML5LocalStorage);
+ this.store_ = new goog.storage.Storage(new goog.storage.mechanism.HTML5LocalStorage());
};
mvc.sync.Local.prototype.getUID = function() {
@@ -18,8 +18,7 @@ mvc.sync.Local.prototype.getUID = function() {
mvc.sync.Local.prototype.create = function(model, callback) {
var id = this.getUID();
- model.set('id') = id;
- this.store_.update(model);
+ model.set('id', id);
};
mvc.sync.Local.prototype.read = function(model, callback) {
View
3 testApp/testApp.html
@@ -9,7 +9,8 @@
<script>
goog.require('goog.events');
goog.require('mvc.Model');
- goog.require('mvc.Model.Schema');
+ goog.require('mvc.sync.Local');
+ goog.require('mvc.model.Schema');
goog.require('mvc.Control');
goog.require('goog.dom');
</script>
View
29 testApp/testApp.js
@@ -1,9 +1,14 @@
-document.write('<div id="note"><div id="text"></div><div id="new_input"><textarea class="new_text"></textarea><textarea class="new_text"></textarea></div></div>');
+document.write('<div id="note"><div><input type="text" id="idinput" class="idinput"/><input type="button" value="get Id" id="idbutton" class="idbutton"></div><div id="text"></div><div id="new_input"><textarea class="new_text"></textarea><textarea class="new_text"></textarea></div></div>');
-var schema = new mvc.model.Schema({'text': /^[a-z]*$/i});
+var sync = new mvc.sync.Local();
-var model = new mvc.Model({'text': 'default'}, schema);
+var schema = new mvc.model.Schema({'text': /\n#/});
+
+var model = new mvc.Model({'text': 'default'}, schema, sync);
+model.save();
+
+goog.dom.getElement("idinput").value = model.get("id");
var control = new mvc.Control(model);
@@ -15,9 +20,21 @@ control.init = function() {
});
this.delegateEvents({
- 'keyup .new_text': function(e) {
- model.set('text', e.target.value);
- }
+ 'keyup .new_text': goog.bind(function(e) {
+ this.getModel().set('text', e.target.value);
+ this.getModel().save();
+ }, this),
+ 'click .idbutton': goog.bind(function(e) {
+ this.getModel().dispose();
+ var id = goog.dom.getElement('idinput').value;
+ var model = new mvc.Model({'id' : id}, schema, sync);
+ model.fetch();
+ model.bind('text', control.getEls('.new_text'), function(el, val) {
+ el.value = val;
+ });
+ this.setModel(model);
+ model.change();
+ }, this)
});
};
View
8 test_deps.js
@@ -3,11 +3,11 @@ goog.addDependency("../../../collection.js", ['mvc.Collection'], ['mvc.Model', '
goog.addDependency("../../../collection_test.js", [], ['mvc.Collection', 'mvc.Model', 'goog.testing.PropertyReplacer', 'goog.testing.jsunit']);
goog.addDependency("../../../control.js", ['mvc.Control'], ['goog.dom', 'goog.object', 'goog.ui.Component']);
goog.addDependency("../../../control_test.js", [], ['mvc.Control', 'goog.dom', 'goog.testing.jsunit']);
-goog.addDependency("../../../model.js", ['mvc.Model', 'mvc.Model.Schema'], ['goog.array', 'goog.dom', 'goog.events', 'goog.events.EventTarget', 'goog.object']);
+goog.addDependency("../../../model.js", ['mvc.Model', 'mvc.model.Schema'], ['goog.array', 'goog.dom', 'goog.events', 'goog.events.EventTarget', 'goog.object']);
goog.addDependency("../../../model_test.js", [], ['mvc.Model', 'goog.testing.jsunit']);
goog.addDependency("../../../router.js", ['mvc.Router'], ['goog.events', 'goog.history']);
goog.addDependency("../../../test_deps.js", [], []);
-goog.addDependency("../../../sync/ajax.js", ['mvc.sync.Ajax'], ['mvc.sync', 'goog.net.XhrIo']);
-goog.addDependency("../../../sync/local.js", ['mvc.sync.local'], []);
-goog.addDependency("../../../sync/sync.js", ['mvc.sync'], []);
+goog.addDependency("../../../sync/ajax.js", ['mvc.sync.Ajax'], ['mvc.sync', 'goog.net.XhrManager']);
+goog.addDependency("../../../sync/local.js", ['mvc.sync.Local'], ['goog.storage.Storage', 'goog.storage.mechanism.HTML5LocalStorage']);
+goog.addDependency("../../../sync/sync.js", ['mvc.ync'], []);
goog.addDependency("../../../testApp/testApp.js", [], []);

0 comments on commit d0c9856

Please sign in to comment.
Something went wrong with that request. Please try again.