Skip to content

Instantly share code, notes, and snippets.

@felixboehm
Created April 26, 2014 17:16
Show Gist options
  • Save felixboehm/11325597 to your computer and use it in GitHub Desktop.
Save felixboehm/11325597 to your computer and use it in GitHub Desktop.
Angularjs Models with Restangular
angular.module('board_app')
.controller('test_controller', function ($scope, Restangular) {
Restangular.extendModel('test_board_lists', function(model) {
model.prettyName = function() {
return "BoardList " + model.name;
};
return model;
});
function Board(model) {
if (!model || !model.hasOwnProperty('restangularCollection')) return Board.create(model);
if (model.board_lists) Restangular.restangularizeCollection(model, model.board_lists, 'test_board_lists');
this.api_url = 'test_boards';
// Defaults
var a = {
name: "Hallo"
}
angular.forEach(a, function(val, key) {
if (! model[key]) model[key] = val;
})
model.prettyName = function() {
return "Board " + model.name;
};
console.log(model);
return model;
}
Board.create = function(data) {
var model = Restangular.one('test_boards');
angular.forEach(data, function(val, key) {
model[key] = val;
})
return model;
}
Restangular.extendModel('test_boards', Board);
var boardPromise = Restangular.one('test_boards', 1).get();
boardPromise.then(function(board) {
board.board_lists[0].name = 'halllooo';
board.board_lists[0].put();
// console.log(board.prettyName(), board.board_lists[0].prettyName());
// console.log(Object.getOwnPropertyNames(board));
});
//console.log(new Board({name: "blub"}).name);
//console.log(Board.create({name: "create"}).name);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment