Skip to content

Instantly share code, notes, and snippets.

@juanmav
Created October 3, 2014 21:51
Show Gist options
  • Save juanmav/2f11b53b2bf65ac8f0a4 to your computer and use it in GitHub Desktop.
Save juanmav/2f11b53b2bf65ac8f0a4 to your computer and use it in GitHub Desktop.
PersistenceJs Example with AngularJS Factory.
// 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);
});
};
});
@SergeS
Copy link

SergeS commented Feb 12, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment