Created
October 3, 2014 21:51
-
-
Save juanmav/2f11b53b2bf65ac8f0a4 to your computer and use it in GitHub Desktop.
PersistenceJs Example with AngularJS Factory.
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
// Task.js | |
var app = angular.module('MyApp'); | |
app.factory('Task', function () { | |
// Hacer un ejeemplo con t odo para depues re-factorizar. | |
//console.debug(persistence.store.websql); | |
persistence.store.websql.config(persistence, 'MyTask', 'A database description', 5 * 1024 * 1024); | |
var Task = persistence.define('Task', { | |
name: "TEXT", | |
description: "TEXT", | |
done: "BOOL" | |
}); | |
Task.prototype.getResume = function (){ | |
return this.id + " " + this.name + " " + this.description + " " + this.done; | |
}; | |
persistence.schemaSync(); | |
return Task; | |
}); | |
// Comment.js | |
'use strict'; | |
var app = angular.module('MyApp') | |
app.factory('Comment', function (Task) { | |
var Comment = persistence.define('Comment', { | |
text : 'TEXT' | |
}); | |
Task.hasMany('comments', Comment, 'task'); | |
return Comment; | |
}); | |
// Example.js | |
var app = angular.module('MyApp'); | |
app.controller('PersistenceCtrl', function ($scope, Task, Comment) { | |
$scope.click = function (){ | |
var tarea = new Task(); | |
//tarea.id = 1; | |
tarea.name = "asdsad"; | |
tarea.description = "descripcion"; | |
tarea.done = true; | |
var comentario = new Comment(); | |
comentario.text = 'lalaala'; | |
tarea.comments.add(comentario); | |
console.debug(tarea.getResume()); | |
persistence.add(tarea); | |
persistence.flush(function() { | |
console.debug('Done flushing'); | |
}); | |
} | |
$scope.recover = function () { | |
console.log('asdasdasd'); | |
//console.log(); | |
Task.all().filter('id', '=', 1).list(null, function(result){ | |
console.log(result); | |
}); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, after few tries with persistence.js I discovered the problem, that you cannot put your model object directly to any angular directive, as it have circular reference.