Last active
December 27, 2015 04:48
-
-
Save ritch/7269020 to your computer and use it in GitHub Desktop.
BACN + LoopBack Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... 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'}); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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