Skip to content

Instantly share code, notes, and snippets.

@ritch
Last active December 27, 2015 04:48
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 ritch/7269020 to your computer and use it in GitHub Desktop.
Save ritch/7269020 to your computer and use it in GitHub Desktop.
BACN + LoopBack Example
// ... imagine backbone boilerplate here... pointing to localhost:3000
var Product = Backbone.Model.extend({
url: '/products'
});
/* would be nice if I could just include this here...
Product.validatesLengthOf('name', {
min: 3,
max: 200,
message: {
min: 'Product name is too short'
max: 'Product name is too long'
}
);
Product.validatesNumericalityOf('price', {
min: 0.01,
max: 999.99,
message: {
min: 'Price must be at least 1 cent',
max: 'Max price is 999.99'
}
});
*/
var FormView = Backbone.View.extend({
el: "#product-form",
initialize: function(){
Backbone.ModelBinding.call(this);
var self = this;
this.$el.find('#save-btn').click(function() {
//
// validation here...?
//
self.model.save();
});
}
});
var pencil = new Product();
var formView = new FormView({model: pencil});
pencil.set({price: 0.99, name: 'pencil'});
<form id="product-form">
<span id="product-err"></span>
name: <input id="name" type="text"><br/>
price: <input id="price" type="text"><br/>
<button id="save-btn">save</button>
</form>
var Product = loopback.Model.extend('product');
Product.validatesLengthOf('name', {
min: 3,
max: 200,
message: {
min: 'Product name is too short'
max: 'Product name is too long'
}
);
Product.validatesNumericalityOf('price', {
min: 0.01,
max: 999.99,
message: {
min: 'Price must be at least 1 cent',
max: 'Max price is 999.99'
}
});
Product.attachTo(loopback.memory());
var app = loopback();
app.model(Product);
app.use(loopback.rest());
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment