From 9737a309c8ccb759b4d35e5a0d78590f04005a08 Mon Sep 17 00:00:00 2001 From: Radek Stepan Date: Sun, 31 Aug 2014 17:22:29 -0700 Subject: [PATCH] use ractive adaptor to interact between instances; github login --- Gruntfile.coffee | 2 +- bower.json | 2 +- public/js/app.bundle.js | 140 +++++++++++++++++++++-------------- public/js/app.js | 12 +-- src/components/header.coffee | 3 - src/modules/firebase.coffee | 7 +- src/modules/user.coffee | 5 +- 7 files changed, 91 insertions(+), 80 deletions(-) diff --git a/Gruntfile.coffee b/Gruntfile.coffee index 274897d..92d12bb 100644 --- a/Gruntfile.coffee +++ b/Gruntfile.coffee @@ -30,7 +30,7 @@ module.exports = (grunt) -> # Vendor dependencies. 'vendor/lodash/dist/lodash.js' 'vendor/ractive/ractive.js' - 'vendor/ractive-plugins/adaptors/Ractive.js' + 'vendor/ractive-adaptor/adaptor.js' 'vendor/firebase/firebase.js' 'vendor/firebase-simple-login/firebase-simple-login.js' # Our app. diff --git a/bower.json b/bower.json index 2245336..e3f8d8d 100644 --- a/bower.json +++ b/bower.json @@ -5,7 +5,7 @@ "lodash": "2.3.0", "normalize-css": "2.1.3", "ractive": "~0.5.5", - "ractive-plugins": "Rich-Harris/Ractive-plugins", + "ractive-adaptor": "radekstepan/ractive-adaptor-ractive", "firebase": "~1.0.21", "firebase-simple-login": "~1.6.3" } diff --git a/public/js/app.bundle.js b/public/js/app.bundle.js index 619b38b..b20e722 100644 --- a/public/js/app.bundle.js +++ b/public/js/app.bundle.js @@ -19974,61 +19974,97 @@ // // Changes to either Ractive will be reflected in both. -(function () { +(function ( global, factory ) { - var RactiveWrapper; + 'use strict'; - Ractive.adaptors.Ractive = { - filter: function ( object ) { - return object instanceof Ractive; - }, - wrap: function ( ractive, otherRactive, keypath, prefixer ) { - return new RactiveWrapper( ractive, otherRactive, keypath, prefixer ); - } - }; + // Common JS (i.e. browserify) environment + if ( typeof module !== 'undefined' && module.exports && typeof require === 'function' ) { + factory( require( 'ractive' ) ); + } - RactiveWrapper = function ( ractive, otherRactive, keypath, prefixer ) { - var wrapper = this; + // AMD? + else if ( typeof define === 'function' && define.amd ) { + define([ 'ractive' ], factory ); + } - this.value = otherRactive; + // browser global + else if ( global.Ractive ) { + factory( global.Ractive ); + } - this.changeHandler = otherRactive.on( 'change', function ( changeHash ) { - wrapper.shortCircuit = true; - ractive.set( prefixer( changeHash ) ); - wrapper.shortCircuit = false; - }); + else { + throw new Error( 'Could not find Ractive! It must be loaded before the ractive-adaptor-ractive plugin' ); + } - this.resetHandler = otherRactive.on( 'reset', function ( newData ) { - wrapper.shortCircuit = true; - ractive.update( keypath ); - wrapper.shortCircuit = false; - }); - }; +}( typeof window !== 'undefined' ? window : this, function ( Ractive ) { - RactiveWrapper.prototype = { - teardown: function () { - this.changeHandler.cancel(); - this.resetHandler.cancel(); - }, - get: function () { - return this.value.get(); - }, - set: function ( keypath, value ) { - this.value.set( keypath, value ); - }, - reset: function ( object ) { - // If the new object is a Backbone model, assume this one is - // being retired. Ditto if it's not a model at all - if ( object instanceof Ractive || typeof object !== 'object' ) { - return false; - } + 'use strict'; - // Otherwise if this is a POJO, reset the model - this.value.reset( object ); - } - }; + if ( !Ractive ) { + throw new Error( 'Could not find Ractive! Check your paths config' ); + } -}());;/* Firebase v1.0.21 */ (function() {var h,aa=this;function n(a){return void 0!==a}function ba(){}function ca(a){a.sb=function(){return a.md?a.md:a.md=new a}} + var RactiveWrapper; + + Ractive.adaptors.Ractive = { + filter: function ( object ) { + return object instanceof Ractive; + }, + wrap: function ( ractive, otherRactive, keypath, prefixer ) { + return new RactiveWrapper( ractive, otherRactive, keypath, prefixer ); + } + }; + + RactiveWrapper = function ( ractive, otherRactive, keypath, prefixer ) { + var wrapper = this; + + this.value = otherRactive; + + this.changeHandler = otherRactive.on( 'change', function ( changeHash ) { + wrapper.setting = true; + ractive.set( prefixer( changeHash ) ); + wrapper.setting = false; + }); + + this.resetHandler = otherRactive.on( 'reset', function ( newData ) { + wrapper.setting = true; + ractive.update( keypath ); + wrapper.setting = false; + }); + }; + + RactiveWrapper.prototype = { + teardown: function () { + this.changeHandler.cancel(); + this.resetHandler.cancel(); + }, + get: function () { + return this.value.get(); + }, + set: function ( keypath, value ) { + // Only set if the we didn't originate the change. + if (!this.setting) { + this.value.set( keypath, value ); + } + }, + reset: function ( object ) { + if (this.setting) { + return; + } + + // If the new object is a Backbone model, assume this one is + // being retired. Ditto if it's not a model at all + if ( object instanceof Ractive || typeof object !== 'object' ) { + return false; + } + + // Otherwise if this is a POJO, reset the model + this.value.reset( object ); + } + }; + +}));;/* Firebase v1.0.21 */ (function() {var h,aa=this;function n(a){return void 0!==a}function ba(){}function ca(a){a.sb=function(){return a.md?a.md:a.md=new a}} function da(a){var b=typeof a;if("object"==b)if(a){if(a instanceof Array)return"array";if(a instanceof Object)return b;var c=Object.prototype.toString.call(a);if("[object Window]"==c)return"object";if("[object Array]"==c||"number"==typeof a.length&&"undefined"!=typeof a.splice&&"undefined"!=typeof a.propertyIsEnumerable&&!a.propertyIsEnumerable("splice"))return"array";if("[object Function]"==c||"undefined"!=typeof a.call&&"undefined"!=typeof a.propertyIsEnumerable&&!a.propertyIsEnumerable("call"))return"function"}else return"null"; else if("function"==b&&"undefined"==typeof a.call)return"object";return b}function ea(a){return"array"==da(a)}function fa(a){var b=da(a);return"array"==b||"object"==b&&"number"==typeof a.length}function q(a){return"string"==typeof a}function ga(a){return"number"==typeof a}function ha(a){var b=typeof a;return"object"==b&&null!=a||"function"==b}function ia(a,b,c){return a.call.apply(a.bind,arguments)} function ja(a,b,c){if(!a)throw Error();if(2","
","","
","","

","","
"," "," "," ","
",""," ",""].join("\n"); + module.exports = ["
","
"," {{#user.displayName}}"," {{user.displayName}} logged in"," {{else}}"," Sign In"," {{/user.displayName}}","
","","

","","
"," "," "," ","
",""," ","
"].join("\n"); }); // layout.mustache diff --git a/public/js/app.js b/public/js/app.js index 1d20e7c..6ed6b0f 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -36,7 +36,6 @@ module.exports = Ractive.extend({ 'template': require('../templates/header'), init: function() { - console.log(this.get('user.uid')); return this.on('login', function() { return firebase.login(function(err) { if (err) { @@ -75,7 +74,6 @@ FB = (function() { function FB() { - console.log('Init Firebase'); this.client = new Firebase("https://" + config.firebase + ".firebaseio.com"); this.auth = new FirebaseSimpleLogin(this.client, function(err, obj) { if (err || !obj) { @@ -118,20 +116,14 @@ // user.coffee root.require.register('burnchart/src/modules/user.js', function(exports, require, module) { - var user; - - module.exports = user = new Ractive(); - - user.observe('uid', function() { - return console.log('User', arguments); - }); + module.exports = new Ractive(); }); // header.mustache root.require.register('burnchart/src/templates/header.js', function(exports, require, module) { - module.exports = ["
","
","","
","","

","","
"," "," "," ","
",""," ","
"].join("\n"); + module.exports = ["
","
"," {{#user.displayName}}"," {{user.displayName}} logged in"," {{else}}"," Sign In"," {{/user.displayName}}","
","","

","","
"," "," "," ","
",""," ","
"].join("\n"); }); // layout.mustache diff --git a/src/components/header.coffee b/src/components/header.coffee index ec5b75b..5f09157 100644 --- a/src/components/header.coffee +++ b/src/components/header.coffee @@ -6,9 +6,6 @@ module.exports = Ractive.extend 'template': require '../templates/header' init: -> - # TODO: how to access adapted ractive model? - console.log @get 'user.uid' - # Login user. @on 'login', -> firebase.login (err) -> diff --git a/src/modules/firebase.coffee b/src/modules/firebase.coffee index 10b08f1..2951703 100644 --- a/src/modules/firebase.coffee +++ b/src/modules/firebase.coffee @@ -6,16 +6,13 @@ authCb = -> class FB - constructor: -> - console.log 'Init Firebase' - + constructor: -> # Setup a new client. @client = new Firebase "https://#{config.firebase}.firebaseio.com" # Check if we have a user in session. @auth = new FirebaseSimpleLogin @client, (err, obj) -> - if err or not obj - return authCb err + return authCb err if err or not obj # Save user. user.set obj diff --git a/src/modules/user.coffee b/src/modules/user.coffee index f0847d1..a345d99 100644 --- a/src/modules/user.coffee +++ b/src/modules/user.coffee @@ -1,5 +1,2 @@ # Currently logged-in user. -module.exports = user = new Ractive() - -user.observe 'uid', -> - console.log 'User', arguments \ No newline at end of file +module.exports = new Ractive() \ No newline at end of file