Skip to content

Instantly share code, notes, and snippets.

@foxdonut
Created March 30, 2015 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foxdonut/a87d5ef9e259c48ae679 to your computer and use it in GitHub Desktop.
Save foxdonut/a87d5ef9e259c48ae679 to your computer and use it in GitHub Desktop.
more wire code
module.exports = function() {
var browser = require("rest/browser");
var mime = require("rest/interceptor/mime");
var client = browser.wrap(mime, { mime: "application/json" });
return client;
};
module.exports = {
//$plugins: [ { module: require("rest/wire") } ],
client: {
create: {
module: require("./client")
}
},
stir: {
create: {
module: require("./stir"),
args: { $ref: "client" }
}
}
};
/* see also: https://github.com/briancavalier/fabulous/blob/master/rest.js */
var when = require("when");
module.exports = function(client) {
return function(baseUrl) {
return {
query: function(params) {
console.log("query:", params);
return client({
method: "GET",
path: baseUrl,
params: params
});
},
get: function(id) {
console.log("get:", id);
return client({
method: "GET",
path: baseUrl + "/" + id
});
},
save: function(model) {
console.log("save:", model);
if (model) {
var request = (model.id) ? {
method: "PUT",
path: baseUrl + "/" + model.id
} : {
method: "POST",
path: baseUrl
};
request.entity = model;
return client(request);
}
return when.reject();
},
"delete": function(model) {
console.log("delete:", model);
return client({
method: "DELETE",
path: baseUrl + "/" + model.id
});
},
getEntity: function(response) {
return response.entity;
}
};
};
};
var ko = require("knockout");
var books = ko.observableArray();
module.exports = {
books: books,
deleteBook: function(book) {
console.log("deleteBook:", book);
books.remove(book);
return book;
},
log: function(value) {
console.log("logged!");
return value;
},
addBook: function(book) {
this.books.push(book);
}
};
var _ = require("lodash");
module.exports = _.extend(
require("../../resource/wire-spec"), {
bookResource: {
create: { $ref: "stir", args: "/books" },
ready: "query"
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment