Comparing version 0.1.7 to 0.1.8
@@ -212,3 +212,3 @@ // Generated by CoffeeScript 1.6.2 | ||
Field.prototype._getDefault = function(value) { | ||
if (!this.options["default"]) { | ||
if (this.options["default"] == null) { | ||
return value; | ||
@@ -215,0 +215,0 @@ } |
{ | ||
"name": "linen", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"description": "```javascript", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
129
README.md
@@ -0,10 +1,23 @@ | ||
Linen (line-in) maps API's to [bindable](/classdojo/bindable.js) `objects`, and `collections`. At [classdojo](http://classdojo.com), we use `linen` to abstract our API from front-end, so we don't necessarily depend on any sort of API while developing new components. This allows us to rapidly build prototypes which can be wired up later. | ||
Here's an example person schema: | ||
```javascript | ||
var linen = require("linen"){}; | ||
var linen = require("linen")(); | ||
linen.addSchema({ | ||
// name of the schema - gets referenced by | ||
// linen.model("person") | ||
name: "person", | ||
//fields for the person. Keep in mind that model properties | ||
//get *validated* against their coresponding field. | ||
fields: { | ||
first_name: "string", | ||
last_name: "string", | ||
full_name: { | ||
firstName: "string", | ||
lastName: "string", | ||
// virtual value - doesn't actually exist in the API | ||
fullName: { | ||
$get: function(model) { | ||
@@ -20,2 +33,5 @@ return model.get("first_name") + " " + model.get("last_name"); | ||
}, | ||
// fetches GET /people/:personId/friends when | ||
// person.bind("friends").to(fn) is called | ||
friends: [{ | ||
@@ -28,2 +44,5 @@ $ref: "person", | ||
}, | ||
//fetches GET /people/:personId when | ||
//person.bind(property).to(fn) is valled | ||
fetch: function(payload, next) { | ||
@@ -33,14 +52,100 @@ transport.fetch(payload.method, "/people/" + (payload.model.get("_id") || ""), next); | ||
}); | ||
``` | ||
var person = linen.model("person", { | ||
first_name: "John", | ||
last_name: "Doe" | ||
To use the schema, simply call the following: | ||
```javascript | ||
var person = linen.model("person"), //creates a new person | ||
existingPerson = linen.model("person", "someId"); //some ID | ||
``` | ||
Linen works by overriding the `bind` method to fetch from any API you have setup, so when you call: | ||
```javascript | ||
//calls GET /people/someId | ||
existingPerson.bind("firstName").to(function(name) { | ||
console.log(name); | ||
}).now(); | ||
``` | ||
The `existingPerson` will asynchronously call `.to(fn)` when it's been loaded from the server. This is useful when data-binding to any sort of UI component, such as [rivets.js](http://rivetsjs.com/), or [paperclip.js](classdojo/paperclip.js). | ||
Here's another example of data-binding to `linen` models: | ||
```javascript | ||
// GET /people/someId/friends | ||
existingPerson.bind("friends").to(function(friends) { | ||
// GET /people/friendId | ||
friends.at(0).bind("firstName").once().to(function(name) { | ||
}).now(); | ||
}).now(); | ||
``` | ||
The above examples make it easy to abstract models from any API. To demonstrate this, here's an example of using `dummy data`: | ||
```javascript | ||
var existingPerson = new bindable.Object({ | ||
firstName: "Ron", | ||
lastName: "Burgundy", | ||
friends: [ | ||
new bindable.Object({ | ||
firstName: "Brian", | ||
lastName: "Fontana" | ||
}), | ||
new bindable.Object({ | ||
firstName: "Brick", | ||
lastName: "Tamland" | ||
}), | ||
new bindable.Object({ | ||
firstName: "Champ", | ||
lastName: "Kind" | ||
}) | ||
] | ||
}); | ||
console.log(person.isNew()); //true | ||
console.log(person.get("full_name")); //John Doe | ||
//POST /people | ||
person.save(function() { | ||
existingPerson.bind("firstName").to(function(name) { | ||
console.log(name); //Ron | ||
}).now(); | ||
existingPerson.bind("friends").to(function(friends) { | ||
friends.at(0).bind("firstName").once().to(function(name) { | ||
console.log(name); //Brian | ||
}).now(); | ||
}).now(); | ||
``` | ||
### API | ||
#### linen() | ||
Returns a new linen instance | ||
#### linen.addSchema(definition) | ||
adds a new schema | ||
#### definition | ||
Inspired by [mongoose](http://mongoosejs.com/): | ||
```javascript | ||
linen.addSchema({ | ||
firstName: { | ||
} | ||
}); | ||
``` | ||
#### linen.model(schemaName[, modelId ]) | ||
returns a new, or existing model | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
66450
35
149