Permalink
Browse files

fix so it compiles

  • Loading branch information...
rhysbrettbowen committed Apr 3, 2012
1 parent 4a8e45b commit d684d42a334b5065f5487b8b79399bf13de1b813
Showing with 161 additions and 101 deletions.
  1. +1 −1 collection.js
  2. +6 −3 mediator.js
  3. +2 −2 model.js
  4. +89 −63 sync/ajax.js
  5. +35 −12 sync/local.js
  6. +28 −20 sync/sync.js
View
@@ -144,7 +144,7 @@ mvc.Collection.prototype.add = function(model, opt_ind, opt_silent) {
}, this);
}
if (goog.isNumber(opt_ind) && opt_ind < 0) {
- opt_ind = this.models.length + opt_ind;
+ opt_ind = this.models_.length + opt_ind;
}
var insert = false;
if (!goog.array.find(this.models_, function(mod) {
View
@@ -86,7 +86,7 @@ mvc.Mediator.prototype.unregister = function(obj, opt_messages) {
* @param {Function|Object} fn to run on message or object of functions to run
* that can include init, fn and dispose.
* @param {Object=} opt_handler to use as 'this' for the function.
- * @return {number} the id to pass to off method.
+ * @return {?number} the id to pass to off method.
*/
mvc.Mediator.prototype.on = function(message, fn, opt_handler) {
if (goog.isArrayLike(message)) {
@@ -101,8 +101,11 @@ mvc.Mediator.prototype.on = function(message, fn, opt_handler) {
fn.init(this.available_[message][0]);
}
}
+ if (goog.isFunction(fn)) {
+ fn = {fn: fn};
+ }
goog.array.insert(this.listeners_[message],
- goog.bind(fn, opt_handler || this));
+ goog.bind(fn.fn, opt_handler || this));
return goog.getUid(fn);
};
@@ -121,7 +124,7 @@ mvc.Mediator.prototype.once = function(message, handler) {
this.off(uid);
},this);
uid = this.on(message, fn);
- return uid;
+ return /** @type {number} */(uid);
};
View
@@ -136,7 +136,7 @@ mvc.Model.prototype.parseSchemaFn_ = function(fn) {
throw new Error();
};
}
- return val;
+ return /** @type {Function} */(val);
};
@@ -535,7 +535,7 @@ mvc.Model.prototype.change_ = function() {
/**
* @param {Function} fn function to run when model is disposed.
- * @param {*=} opt_handler object for 'this' of function.
+ * @param {Object=} opt_handler object for 'this' of function.
* @return {number} id to use for unbind.
*/
mvc.Model.prototype.bindUnload = function(fn, opt_handler) {
View
@@ -7,136 +7,162 @@
goog.provide('mvc.AjaxSync');
+goog.require('goog.Uri.QueryData');
+goog.require('goog.net.XhrManager');
goog.require('mvc.Sync');
-goog.require('goog.net.XhrManager');
-goog.require('goog.Uri.QueryData');
+
/**
* @constructor
* @implements {mvc.Sync}
+ * @param {Object|string|Function} url used to construct URLs.
*/
mvc.AjaxSync = function(url) {
- var baseFunction = function(model){return "";};
-
- this.baseUrls_ = {
- create: baseFunction,
- read: baseFunction,
- update: baseFunction,
- del: baseFunction
+ var baseFunction = function(model) {return '';};
+
+ this.baseUrls_ = {
+ create: baseFunction,
+ read: baseFunction,
+ update: baseFunction,
+ del: baseFunction
+ };
+
+ if (goog.isString(url) || goog.isFunction(url)) {
+ url = {
+ create: url,
+ read: url,
+ update: url,
+ del: url
};
-
- if(goog.isString(url) || goog.isFunction(url)) {
- url = {
- create: url,
- read: url,
- update: url,
- del: url
- };
- }
- goog.object.extend(this.baseUrls_, goog.object.map(url, this.urlifyString));
- this.xhr_ = new goog.net.XhrManager();
- this.sendCount_ = 0;
+ }
+ goog.object.extend(this.baseUrls_, goog.object.map(url, this.urlifyString));
+ this.xhr_ = new goog.net.XhrManager();
+ this.sendCount_ = 0;
};
+
/**
* takes a string defining a url where :attribute will return that models
* attribute. e.g.
- *
+ *
* var obj = new mvc.Model({attrs:{'id': 'fred'}});
* var urlGen = urlifyString("/object=:id/blah");
* urlGen(obj); // returns "/object=fred/blah"
- *
*
- * @param {string} val
- * @return {function(mvc.Model):string}
+ *
+ * @param {string} val to be changed to a function.
+ * @return {function(mvc.Model):string} function that gives URL.
*/
mvc.AjaxSync.prototype.urlifyString = function(val) {
- if(goog.isString(val))
- return function(model) {
- return val.replace(/:(\w+)/g, function(id) {
- return model.get(id.substring(1));
- });};
- return /** @type {function(mvc.Model):string} */(val);
+ if (goog.isString(val)) {
+ return function(model) {
+ return val.replace(/:(\w+)/g,
+ function(id) {
+ return model.get(id.substring(1));
+ });
+ };
+ }
+ return /** @type {function(mvc.Model):string} */(val);
};
+
/**
* @inheritDoc
*/
-mvc.AjaxSync.prototype.create = function(model, callback) {
+mvc.AjaxSync.prototype.create = function(model, opt_callback) {
- this.xhr_.send(""+(this.sendCount_++), this.baseUrls_.create(model),
- "POST", goog.Uri.QueryData.createFromMap(model.toJson()).toString(),
- undefined, undefined,
- goog.bind(this.onCreateComplete_, this, model, callback));
+ this.xhr_.send('' + (this.sendCount_++), this.baseUrls_.create(model),
+ 'POST', goog.Uri.QueryData.createFromMap(model.toJson()).toString(),
+ undefined, undefined,
+ goog.bind(this.onCreateComplete_, this, model,
+ (opt_callback || goog.nullFunction)));
};
+
/**
* @inheritDoc
*/
-mvc.AjaxSync.prototype.read = function(model, callback) {
- this.xhr_.send(""+(this.sendCount_++), this.baseUrls_.read(model),
- "GET", undefined, undefined, undefined,
- goog.bind(this.onReadComplete_, this, model, callback));
+mvc.AjaxSync.prototype.read = function(model, opt_callback) {
+ this.xhr_.send('' + (this.sendCount_++), this.baseUrls_.read(model),
+ 'GET', undefined, undefined, undefined,
+ goog.bind(this.onReadComplete_, this, model,
+ (opt_callback || goog.nullFunction)));
};
+
/**
* @inheritDoc
*/
-mvc.AjaxSync.prototype.update = function(model, callback) {
- this.xhr_.send(""+(this.sendCount_++), this.baseUrls_.update(model),
- "PUT", goog.Uri.QueryData.createFromMap(model.toJson()).toString(),
- {'Content-Type' : 'application/x-www-form-urlencoded'}, undefined,
- goog.bind(this.onUpdateComplete_, this, model, callback));
+mvc.AjaxSync.prototype.update = function(model, opt_callback) {
+ this.xhr_.send('' + (this.sendCount_++), this.baseUrls_.update(model),
+ 'PUT', goog.Uri.QueryData.createFromMap(model.toJson()).toString(),
+ {'Content-Type' : 'application/x-www-form-urlencoded'}, undefined,
+ goog.bind(this.onUpdateComplete_, this, model,
+ (opt_callback || goog.nullFunction)));
};
+
/**
* @inheritDoc
*/
-mvc.AjaxSync.prototype.del = function(model, callback) {
- this.xhr_.send(""+(this.sendCount_++), this.baseUrls_.del(model),
- "DELETE", undefined, undefined, undefined,
- goog.bind(this.onDelComplete_, this, model, callback));
+mvc.AjaxSync.prototype.del = function(model, opt_callback) {
+ this.xhr_.send('' + (this.sendCount_++), this.baseUrls_.del(model),
+ 'DELETE', undefined, undefined, undefined,
+ goog.bind(this.onDelComplete_, this, model,
+ (opt_callback || goog.nullFunction)));
};
+
/**
* override this to do processing on returned data
*
- * @param {mvc.Model} model
- * @param {Function} callback
+ * @private
+ * @param {mvc.Model} model being processed.
+ * @param {Function} callback to be called when done.
+ * @param {goog.events.Event} e the completed xhr event.
*/
mvc.AjaxSync.prototype.onCreateComplete_ = function(model, callback, e) {
- var xhr = e.target;
- model.set('id', xhr.getResponseJson()['result']['id']);
+ var xhr = e.target;
+ model.set('id', xhr.getResponseJson()['result']['id']);
};
+
/**
* override this to do processing on returned data
*
- * @param {mvc.Model} model
- * @param {Function} callback
+ * @private
+ * @param {mvc.Model} model being processed.
+ * @param {Function} callback to be called when done.
+ * @param {goog.events.Event} e the completed xhr event.
*/
mvc.AjaxSync.prototype.onReadComplete_ = function(model, callback, e) {
- var xhr = e.target;
- var json = xhr.getResponseJson()['result'];
- model.set(json);
+ var xhr = e.target;
+ var json = xhr.getResponseJson()['result'];
+ model.set(json);
};
+
/**
* override this to do processing on returned data
*
- * @param {mvc.Model} model
- * @param {Function} callback
+ * @private
+ * @param {mvc.Model} model being processed.
+ * @param {Function} callback to be called when done.
+ * @param {Event} e the completed xhr event.
*/
mvc.AjaxSync.prototype.onUpdateComplete_ = function(model, callback, e) {
};
+
/**
* override this to do processing on returned data
*
- * @param {mvc.Model} model
- * @param {Function} callback
+ * @private
+ * @param {mvc.Model} model being processed.
+ * @param {Function} callback to be called when done.
+ * @param {Event} e the completed xhr event.
*/
mvc.AjaxSync.prototype.onDelComplete_ = function(model, callback, e) {
-};
+};
View
@@ -9,32 +9,55 @@ goog.provide('mvc.LocalSync');
goog.require('goog.storage.Storage');
goog.require('goog.storage.mechanism.HTML5LocalStorage');
+
+
/**
* @constructor
* @implements {mvc.Sync}
*/
mvc.LocalSync = function() {
- this.store_ = new goog.storage.Storage(new goog.storage.mechanism.HTML5LocalStorage());
+ this.store_ = new goog.storage.Storage(
+ new goog.storage.mechanism.HTML5LocalStorage());
};
+
+/**
+ * @return {string} uid for object.
+ */
mvc.LocalSync.prototype.getUID = function() {
- this.counter_ = this.counter_||0;
- return (this.counter_++)+"|"+parseInt((new Date()).getTime(), 36);
+ this.counter_ = this.counter_ || 0;
+ return (this.counter_++) + '|' + parseInt((new Date()).getTime(), 36);
};
-mvc.LocalSync.prototype.create = function(model, callback) {
- var id = this.getUID();
- model.set('id', id);
+
+/**
+ * @inheritDoc
+ */
+mvc.LocalSync.prototype.create = function(model, opt_callback) {
+ var id = this.getUID();
+ model.set('id', id);
};
-mvc.LocalSync.prototype.read = function(model, callback) {
- model.set(this.store_.get(model.get('id')));
+
+/**
+ * @inheritDoc
+ */
+mvc.LocalSync.prototype.read = function(model, opt_callback) {
+ model.set(this.store_.get(model.get('id')));
};
-mvc.LocalSync.prototype.update = function(model, callback) {
- this.store_.set(model.get('id'), model.toJson());
+
+/**
+ * @inheritDoc
+ */
+mvc.LocalSync.prototype.update = function(model, opt_callback) {
+ this.store_.set(model.get('id'), model.toJson());
};
-mvc.LocalSync.prototype.del = function(model, callback) {
- this.store_.remove(model.get('id'));
+
+/**
+ * @inheritDoc
+ */
+mvc.LocalSync.prototype.del = function(model, opt_callback) {
+ this.store_.remove(model.get('id'));
};
Oops, something went wrong.

0 comments on commit d684d42

Please sign in to comment.