mikekelly / backbone.hal Goto Github PK
View Code? Open in Web Editor NEWHypermedia for Backbone.js
License: MIT License
Hypermedia for Backbone.js
License: MIT License
Is there a particular reason for not having Model.clear()
reset embedded/links properties just like Collection.reset()
does for collections? I've been in the need for clearing a model's links/embedded attributes a few times now and doing manually feels pretty repetitive now, specially since I already call clear
to get rid of the attributes.
Monkey = HAL.Model.extend({});
Monkeys = HAL.Collection.extend({ model: Monkey });
monkeys_list = [
{
"_links": {
"self": {
"href": "/zoo/monkeys/1"
}
},
"id": "1",
"name": "Petunia",
"traits": [
"throws poo",
"hosts tea parties"
],
"_embeded": {
"events": [
{
"_links": {
"self": {
"href": "/zoo/events/343"
}
},
"date": "2012-09-11",
"description": "Threw a tea party for the Queen. After sharing some particularly lovely shortbread biscuits, started throwing poo."
},
{
"_links": {
"self": {
"href": "/zoo/events/301"
}
},
"date": "2011-12-19",
"description": "Gave a lovely teapot to a child for Chistmas"
}
]
}
},
{
"_links": {
"self": {
"href": "/zoo/monkeys/3"
}
},
"id": "3",
"name": "Lancelot",
"traits": [
"aspires to be a space monkey",
"dabbles in astronomy and astrophysics"
],
"_embeded": {
"events": [
{
"_links": {
"self": {
"href": "/zoo/events/444"
}
},
"date": "2013-03-28",
"description": "Acted as copilot on the Soyuz TMA-08M mission to the ISS."
},
{
"_links": {
"self": {
"href": "/zoo/events/45"
}
},
"date": "2009-07-02",
"description": "Repremanded after firing a prototype rocket into the nearby Gazelle aviary."
}
]
}
}
];
> monkeys = new Monkeys(monkey_list)
child {links: Object, embedded: Object, attributes: Array[2], length: 0, models: Array[0]…}
> monkeys.models
[]
> monkeys.length
0
If the Monkey model and collection were standard Backbone objects (instead of HAL objects), you'd get the following:
> monkeys.models
[ child, child ]
> monkeys.length
2
Resetting the collection with the JSON array works as expected:
> monkeys = new Monkeys()
> monkeys.reset(monkeys_list)
child {links: Object, embedded: Object, attributes: Array[2], length: 2, models: Array[2]…}
> monkeys.models
[ child, child]
> monkeys.length
2
We at VersionEye are working hard to keep up the quality of the bower's registry.
We just finished our initial analysis of the quality of the Bower.io registry:
7530 - registered packages, 224 of them doesnt exists anymore;
We analysed 7306 existing packages and 1070 of them don't have bower.json on the master branch ( that's where a Bower client pulls a data ).
Sadly, your library mikekelly/backbone.hal
is one of them.
Can you spare 15 minutes to help us to make Bower better?
Just add a new file bower.json
and change attributes.
{
"name": "mikekelly/backbone.hal",
"version": "1.0.0",
"main": "path/to/main.css",
"description": "please add it",
"license": "Eclipse",
"ignore": [
".jshintrc",
"**/*.txt"
],
"dependencies": {
"<dependency_name>": "<semantic_version>",
"<dependency_name>": "<Local_folder>",
"<dependency_name>": "<package>"
},
"devDependencies": {
"<test-framework-name>": "<version>"
}
}
Read more about bower.json on the official spefication and nodejs semver library has great examples of proper versioning.
NB! Please validate your bower.json with jsonlint before commiting your updates.
Thank you!
Timo,
twitter: @versioneye
email: [email protected]
VersionEye - no more legacy software!
Could you please explain the architecture of this plugin? Backbone is based on the concept of an id per model object and composition of resource URLs based on this id plus a base URL. How do you override this behavior? Does this cause any undesirable side-effects in Backbone apps?
Also it would be helpful to have a more substantial example of this approach - sort of a sample app.
My json collection was not named "items" and this required a little debugging to figure out.
We have a HATEAOS API that returns a JSON object of attributes when passed a HTTP OPTIONS request.
im hoping you may be able to help with
Happy to provide more input if needed
I'm trying to use this library, and it's pretty broken.
Basically, it doesn't perform the way you'd expect a typical Backbone collection application to perform.
When fetching the data, the collection should return an array of models and because Hal.Collection overrides the reset method and tries to parse the cloned object out, the models key doesn't get set.
(function(app, Foo) {
Foo.Model = HAL.Model.extend();
Foo.Collection = HAL.Collection.extend({
model: Foo.Model,
url: '/api/foo',
itemRel: 'foos'
});
Foo.Views.List = Backbone.View.extend({
el: '#foos',
template: _.template($('#foos-list').html()),
render: function() {
var self = this,
foos = new Foo.Collection();
foos.fetch({
success: function(collection) {
// explodes here!
$(self.el).html(self.template({ foos: collection.models }));
}
});
}
});
Foo.Router = Backbone.Router.extend({
routes: {
'foos': 'getAll'
},
getAll: function() {
var list = new Foo.Views.List();
list.render();
}
});
var router = new Foo.Router();
})(app, app.module('foo'));
Am I doing this wrong or is this a bug?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.