entangled.js in entangled-0.0.16 vs entangled.js in entangled-0.0.17

- old
+ new

@@ -24,47 +24,87 @@ // $save() will send a request to the server // to either create a new record or update // an existing, depending on whether or not // an id is present. Resource.prototype.$save = function(callback) { - console.log('Saving...'); - var that = this; - console.log(that); if (this.id) { // Update - console.log('Updating...'); var socket = new WebSocket(that.webSocketUrl + '/' + that.id + '/update'); socket.onopen = function() { socket.send(JSON.stringify(that)); + }; - if (callback) callback(); + // Receive updated resource from server + socket.onmessage = function(event) { + if (event.data) { + var data = JSON.parse(event.data); - console.log('Updated'); + // Assign/override new data (such as updated_at, etc) + if (data.resource) { + for (key in data.resource) { + that[key] = data.resource[key]; + } + } + } + + // Pass 'that' to callback for create + // function so that the create function + // can pass the created resource to its + // own callback; not needed for $save per se + if (callback) callback(that); }; } else { // Create - console.log('Creating...'); var socket = new WebSocket(that.webSocketUrl + '/create'); + // Send attributes to server socket.onopen = function() { - console.log(socket); socket.send(JSON.stringify(that)); + }; - if (callback) callback(); + // Receive saved resource from server + socket.onmessage = function(event) { + if (event.data) { + var data = JSON.parse(event.data); - console.log('Created'); + // Assign/override new data (such as id, created_at, + // updated_at, etc) + if (data.resource) { + for (key in data.resource) { + that[key] = data.resource[key]; + } + } + } + + // Pass 'that' to callback for create + // function so that the create function + // can pass the created resource to its + // own callback; not needed for $save per se + if (callback) callback(that); }; } }; + // $update() updates a record in place + Resource.prototype.$update = function(params, callback) { + // Update object in memory + for (var key in params) { + // Skip inherent object properties + if (params.hasOwnProperty(key)) { + this[key] = params[key]; + } + } + + // Save object + this.$save(callback); + }; + // $destroy() will send a request to the server to // destroy an existing record. Resource.prototype.$destroy = function(callback) { - console.log('From $destroy: ', this); - var socket = new WebSocket(this.webSocketUrl + '/' + this.id + '/destroy'); socket.onopen = function() { // It's fine to send an empty message since the // socket's URL contains all the information // needed to destroy the record (the id). @@ -72,10 +112,30 @@ if (callback) callback(); }; }; + // $valid() checks if any errors are attached to the object + // and return false if so, false otherwise. This doesn't actually + // invoke server side validations, so it should only be used + // after calling $save() to check if the record was successfully + // stored in the database + Resource.prototype.$valid = function() { + return !(this.errors && Object.keys(this.errors).length); + }; + + // $invalid() returns the opposite of $valid() + Resource.prototype.$invalid = function() { + return !this.$valid(); + }; + + // $persisted() checks if the record was successfully stored + // in the back end's database + Resource.prototype.$persisted = function() { + return !!this.id; + }; + // Resources wraps all individual Resource objects // in a collection. var Resources = function(resources, webSocketUrl) { this.all = []; @@ -117,26 +177,20 @@ // Convert message to JSON var data = JSON.parse(event.data); // If the collection of Resources was sent if (data.resources) { - console.log('Index'); - // Store retrieved Resources in property this.resources = new Resources(data.resources, socket.url); } else if (data.action) { if (data.action === 'create') { // If new Resource was created, add it to the // collection - console.log('Created'); - console.log(this.resources); - this.resources.all.push(new Resource(data.resource, socket.url)); } else if (data.action === 'update') { // If an existing Resource was updated, // update it in the collection as well - console.log('Updated'); var index; for (var i = 0; i < this.resources.all.length; i++) { if (this.resources.all[i].id === data.resource.id) { index = i; @@ -145,22 +199,21 @@ this.resources.all[index] = new Resource(data.resource, socket.url); } else if (data.action === 'destroy') { // If a Resource was destroyed, remove it // from Resources as well - console.log('Destroyed'); var index; for (var i = 0; i < this.resources.all.length; i++) { if (this.resources.all[i].id === data.resource.id) { index = i; } } this.resources.all.splice(index, 1); } else { - console.log('Something else happened...'); + console.log('Something else other than CRUD happened...'); console.log(data); } } } @@ -168,41 +221,42 @@ // resulting collection callback(this.resources.all); }; }; + // Instantiate and persist a record in one go + Entangled.prototype.create = function(params, callback) { + var resource = this.new(params); + resource.$save(callback); + }; + // Find an individual Resource on the server Entangled.prototype.find = function(id, callback) { var webSocketUrl = this.webSocketUrl; var socket = new WebSocket(webSocketUrl + '/' + id); socket.onmessage = function(event) { // If the message from the server isn't empty if (event.data.length) { // Parse message and convert to JSON var data = JSON.parse(event.data); - console.log('Show'); if (data.resource && !data.action) { // If the Resource was sent from the server, // store it this.resource = new Resource(data.resource, webSocketUrl); } else if (data.action) { if (data.action === 'update') { // If the stored Resource was updated, // update it here as well - console.log('updated!'); - this.resource = new Resource(data.resource, webSocketUrl); } else if (data.action === 'destroy') { // If the stored Resource was destroyed, // remove it from here as well - console.log('destroyed!'); - this.resource = undefined; } } else { - console.log('something else happened...'); + console.log('Something else other than CRUD happened...'); console.log(data); } } // Run callback with retrieved Resource