Skip to content

Commit

Permalink
some jsdocking
Browse files Browse the repository at this point in the history
  • Loading branch information
shackbarth committed Aug 29, 2012
1 parent 083c698 commit 896f78b
Showing 1 changed file with 59 additions and 4 deletions.
63 changes: 59 additions & 4 deletions lib/ext/administrative_route.js
Expand Up @@ -4,11 +4,27 @@ regexp:true, undef:true, strict:true, trailing:true, white:true */

(function () {
"use strict";

var _ = X._, _fs = X.fs, _path = X.path;

X.AdministrativeRoute = X.Route.extend({

/**
Administrative route
@class
@extends X.Route
*/
X.AdministrativeRoute = X.Route.extend(/** @lends X.AdministrativeRoute */{
/**
The name of the model that this route will operate on.
@type {String}
*/
clientModel: "",

/**
Init. Sets this route to handle /%@ and other paths you get when you hold
down the shift key and start typing.
*/
init: function () {
var model = this.get("clientModel"), handles = [];
handles.push("/%@/:id".f(model));
Expand All @@ -17,6 +33,11 @@ regexp:true, undef:true, strict:true, trailing:true, white:true */
this.set("handles", handles);
this._super.init.call(this);
},
/**
Sends requests to the appropriate function
@param {X.Reponse} xtr
*/
handle: function (xtr) {
var clientModel = this.get("clientModel"), method = xtr.get("method"),
data = xtr.get("json");
Expand All @@ -32,11 +53,25 @@ regexp:true, undef:true, strict:true, trailing:true, white:true */
} else this.new.apply(this, arguments);
} else this.fetch.apply(this, arguments);
},

/**
Fetches a mongo model by ID.
@param {X.Reponse} xtr
@param {Number} id
*/
fetch: function (xtr, id) {
var model = this.get("model"), data;
if (id) model.find({_id: id}, _.bind(this.didFind, this, xtr));
else model.find({}, _.bind(this.didFind, this, xtr));
},

/**
Looks up a single mongo model based on specified query.
@param {X.Reponse} xtr
@param {Object} data The specified query in Mongoosish
*/
lookup: function (xtr, data) {
var K = this.get("model");
K.findOne(data, function (err, res) {
Expand All @@ -45,6 +80,13 @@ regexp:true, undef:true, strict:true, trailing:true, white:true */
xtr.write(res).close();
});
},

/**
Updates a model based on data in xtr.get("json");
@param {X.Response} xtr
@param {Number} id The id to update
*/
update: function (xtr, id) {
var K = this.get("model"), data = xtr.get("json");
K.findById(id, function (err, k) {
Expand All @@ -59,13 +101,26 @@ regexp:true, undef:true, strict:true, trailing:true, white:true */
})
});
},

/**
Deletes the ID passed in
@param {X.Response} xtr
@param {Number} id The ID to delete
*/
delete: function (xtr, id) {
var K = this.get("model");
K.remove({_id: id}, function (err) {
if (err) return xtr.error({isError: true, reason: err});
xtr.close();
});
},

/**
Creates a new model based on data in xtr.get("json")
@param {X.Response} xtr
*/
new: function (xtr) {
var K = this.get("model"), k;
k = new K(xtr.get("json"));
Expand All @@ -84,5 +139,5 @@ regexp:true, undef:true, strict:true, trailing:true, white:true */
},
className: "X.AdministrativeRoute"
});

}());

0 comments on commit 896f78b

Please sign in to comment.