Skip to content

Commit

Permalink
Fixes for Firebase integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir Nathoo authored and Amir Nathoo committed Jun 3, 2012
1 parent dd7ccbc commit 093fa03
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 126 deletions.
1 change: 0 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<html>
<head>
<title>Wine Box</title>
<script src="https://trigger.io/catalyst/target/target-script-min.js#62BD64A8-990B-414C-9CB7-E8A462B5B10A"></script>
<script type='text/javascript' src='lib/firebase.js'></script>
<script src="lib/zepto.js"></script>
<script src="lib/underscore.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ forge.tabbar.addButton({
});

$ = $ || Zepto;
wine.util.initialize();
wine.initialize();
30 changes: 15 additions & 15 deletions views.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ wine.views.Picture = Backbone.View.extend({
render: function() {
var el = this.el;
$(el).html($('#tmpl-picture').text());
wine.util.disclosure_indicator(el);
wine.disclosure_indicator(el);
if (wine.photos.length) {
var src = wine.photos.at(0).get('url');
$(el).css('background-image', 'url('+src+')');
Expand All @@ -19,7 +19,7 @@ wine.views.Picture = Backbone.View.extend({
return this;
},
show: function () {
wine.util.resetCurrentView(this);
wine.resetCurrentView(this);
$('#rate_container').show();
$('#rate_container').append(this.el);
},
Expand Down Expand Up @@ -63,14 +63,14 @@ wine.views.Rate = Backbone.View.extend({
this.rate();
} else {
$('.ratephoto', el).html($('#tmpl-rate2').text());
wine.util.disclosure_indicator(el);
wine.disclosure_indicator(el);
fake_active($('.ratephoto', el));
$('.ratephoto', el).bind(clickEvent, this.rate);
}
return this;
},
show: function () {
wine.util.resetCurrentView(this);
wine.resetCurrentView(this);
$('#rate_container').show();
$('#rate_container').append(this.el);
forge.topbar.addButton({
Expand Down Expand Up @@ -109,12 +109,12 @@ wine.views.Rate = Backbone.View.extend({
forge.logging.log('... Set rating');
var rating = parseInt($(ev.target).parent().attr('class').split('_')[1])
forge.logging.log(rating);
wine.util.handleRatingClick(rating, $('#rate'));
wine.handleRatingClick(rating, $('#rate'));
state.get('currentPhoto').set('rating', rating);
addSaveButton();
});
if (state.get('currentPhoto').get('rating')) {
wine.util.handleRatingClick(state.get('currentPhoto').get('rating'), $('#rate'));
wine.handleRatingClick(state.get('currentPhoto').get('rating'), $('#rate'));
addSaveButton();
}
});
Expand Down Expand Up @@ -159,22 +159,22 @@ wine.views.List = Backbone.View.extend({
var rating = parseInt($(item).text());
$(item).html(Mustache.render($('#tmpl-stars').text(), { src: src }));
fake_active($(item).parent());
wine.util.handleRatingClick(rating, $(item));
wine.handleRatingClick(rating, $(item));
var photo = wine.photos.at(idx);
$(item).parent().bind(clickEvent, function() {
wine.router.navigate('detail/'+wine.photos.indexOf(photo), { trigger: true });
$('#list').hide();
});
});
});
wine.util.showDetailIcon(el);
wine.showDetailIcon(el);
},
close: function() {
$('#list_container').hide();
$('#list').hide();
},
show: function () {
wine.util.resetCurrentView(this);
wine.resetCurrentView(this);
$('#list_container').show();
$('#list').show();
forge.topbar.addButton({
Expand All @@ -196,7 +196,7 @@ wine.views.List = Backbone.View.extend({
wine.photos.forEach(function(item) {
if (!item.has('location')) {
forge.logging.log('... Getting location');
wine.util.getLocation(item.get('position'), item.get('timestamp'));
wine.getLocation(item.get('position'), item.get('timestamp'));
}
});
}
Expand All @@ -221,10 +221,10 @@ wine.views.Detail = Backbone.View.extend({
$('.ratephoto', el).each(function(idx, item) {
var rating = parseInt($(item).text());
$(item).html(Mustache.render($('#tmpl-stars').text(), { src: src }));
wine.util.handleRatingClick(rating, $(item));
wine.handleRatingClick(rating, $(item));
});
});
wine.util.showDetailIcon(el);
wine.showDetailIcon(el);
$(el).append('<img class="detail" src="'+src+'" />');
$('.step', el).bind(clickEvent, function() {
wine.router.navigate('mapTab/'+state.get('idx'), { trigger: true });
Expand All @@ -234,7 +234,7 @@ wine.views.Detail = Backbone.View.extend({
return this;
},
show: function () {
wine.util.resetCurrentView(this);
wine.resetCurrentView(this);
$('#list_container').show();
$('#list_container').append(this.el);
forge.topbar.setTitle('Wine Detail');
Expand Down Expand Up @@ -271,7 +271,7 @@ wine.views.Map = Backbone.View.extend({
var el = this.el;
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://maps.googleapis.com/maps/api/js?key=AIzaSyAlFSCee70OJOiD7k-fz8e6ywXVVIkWErU&sensor=true&callback=wine.util.initMap";
script.src = "http://maps.googleapis.com/maps/api/js?key=AIzaSyAlFSCee70OJOiD7k-fz8e6ywXVVIkWErU&sensor=true&callback=wine.initMap";
document.body.appendChild(script);
$('#map_container').append(el);
return this;
Expand Down Expand Up @@ -320,7 +320,7 @@ wine.views.Map = Backbone.View.extend({
},
show: function(idx) {
$('#map_container').show();
wine.util.resetCurrentView(this);
wine.resetCurrentView(this);
if (state.get('map').gmap) {
google.maps.event.trigger(state.get('map').gmap, 'resize');
var currentLatLng = new google.maps.LatLng(state.get('currentCoords').latitude, state.get('currentCoords').longitude, true);
Expand Down
213 changes: 104 additions & 109 deletions wine.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
forge.enableDebug();
//TODO - test migration, test add photo, test synching between phones, test deletion sycnhing, test offline

//Fake support of :active on Android
var fake_active = function(el) {
if (forge.is.android() && $(el).hasClass('listenactive')) {
Expand Down Expand Up @@ -42,122 +39,121 @@ var wine = {
publicFirebase: null,
userFirebase: null,
user: null,
util: {
initialize: function() {
forge.logging.log('Initializing...');
wine.util.initUser(function() {
wine.util.initStorage();
wine.util.preRenderViews();
Backbone.history.start();
if (forge.is.mobile()) {
if (state.get('rateButton')) {
wine.router.navigate("rateTab", { trigger: true});
forge.logging.log('... completed initialization');
} else {
window.initInterval = setInterval(function() {
if (state.get('rateButton')) {
wine.router.navigate("rateTab", { trigger: true});
forge.logging.log('... completed initialization');
clearInterval(window.initInterval);
}
initialize: function() {
forge.logging.log('Initializing...');
wine.initUser(function() {
wine.initStorage();
wine.preRenderViews();
Backbone.history.start();
if (forge.is.mobile()) {
if (state.get('rateButton')) {
wine.router.navigate("rateTab", { trigger: true});
forge.logging.log('... completed initialization');
} else {
window.initInterval = setInterval(function() {
if (state.get('rateButton')) {
wine.router.navigate("rateTab", { trigger: true});
forge.logging.log('... completed initialization');
clearInterval(window.initInterval);
}
}, 200);
}
}
if (forge.is.web()) {
wine.router.navigate("listTab", { trigger: true});
}
});
},
initUser: function(cb) {
forge.prefs.get('user', function(user) {
wine.user = user || wine.util.getUUID();
forge.prefs.set('user', wine.user);
cb();
});
},
getUUID: function() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0;
var v = c == "x" ? r : (r & 0x3 | 0x8);
return v.toString(16);
}).toUpperCase();
},
initStorage: function() {
//Initialize Firebase
wine.publicFirebase = new Firebase('http://gamma.firebase.com/winebox/public');
wine.userFirebase = new Firebase('http://gamma.firebase.com/winebox').child(wine.user);
//Initialize collection
if (localStorage.wine) {
//Handle migration
wine.photos = new wine.collections.Photos(JSON.parse(localStoreage.wine));
wine.photos.forEach(function(photo) {
photo.set('user', wine.user);
wine.publicFirebase.child(photo.get('timestamp')).set(photo.toJSON());
wine.userFirebase.child(photo.get('timestamp')).set(photo.toJSON());
});
localStorage.clear();
} else {
wine.photos = new wine.collections.Photos();
wine.userFirebase.once('value', function(snapshot) {
snapshot.forEach(function(photo) {
wine.photos.add(new wine.models.Photo(photo.val()));
});
});

}
//Add event handlers
wine.photos.on("add", function(photo) {
});
},
initUser: function(cb) {
forge.prefs.get('user', function(user) {
wine.user = user || wine.getUUID();
forge.prefs.set('user', wine.user);
cb();
});
},
getUUID: function() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0;
var v = c == "x" ? r : (r & 0x3 | 0x8);
return v.toString(16);
}).toUpperCase();
},
initStorage: function() {
//Initialize Firebase
wine.publicFirebase = new Firebase('http://gamma.firebase.com/winebox/public');
wine.userFirebase = new Firebase('http://gamma.firebase.com/winebox').child(wine.user);
//Initialize collection
if (localStorage.wine) {
//Handle migration
wine.photos = new wine.collections.Photos(JSON.parse(localStorage.wine));
wine.photos.forEach(function(photo) {
photo.set('user', wine.user);
state.get('list').add(photo);
wine.publicFirebase.child(photo.get('timestamp')).set(photo.toJSON());
wine.userFirebase.child(photo.get('timestamp')).set(photo.toJSON());
});
wine.photos.on("remove", function() {
state.get('list').removeByIndex(state.get('idx'));
wine.publicFirebase.child(this.get('timestamp')).remove();
wine.userFirebase.child(this.get('timestamp')).remove();
});
wine.userFirebase.on("child_added", function(child) {
if (child.user === wine.user && !state.get('list').exists(child.timestamp)) {
var photo = wine.models.Photo(child);
state.get('list').add(photo);
}
});
wine.userFirebase.on("child_removed", function(child) {
if (child.user === wine.user) {
state.get('list').removeByTimestamp(child.get(timestamp));
}
});
},
preRenderViews: function() {
state.set('list', new wine.views.List());
state.get('list').render();
forge.logging.log('Pre-rendered wine list');
state.set('map', new wine.views.Map());
state.get('map').render();
forge.logging.log('Pre-rendered map');
},
disclosure_indicator: function(el) {
forge.tools.getURL('img/disclosure_indicator.png', function(src) {
$('img.icon', el).attr('src', src);
});
},
handleRatingClick: function(rating, el) {
$('label', el).removeClass('no_star');
$('label', el).each(function(idx, el) {
if (parseInt($(el).attr('class').split('_')[1]) > rating) {
$(el).addClass('no_star');
}
localStorage.clear();
} else {
wine.photos = new wine.collections.Photos();
wine.userFirebase.once('value', function(snapshot) {
snapshot.forEach(function(photo) {
wine.photos.add(new wine.models.Photo(photo.val()));
});
});
},
getLocation: function(coords, timestamp) {
forge.request.ajax({
url: "http://maps.googleapis.com/maps/api/geocode/json?latlng="+coords.latitude+","+coords.longitude+"&sensor=true",
dataType: "json",
success: function(response) {
try {
var photo = wine.photos.filter(function(item) {
return item.get('timestamp') == timestamp;

}
//Add event handlers
wine.photos.on("add", function(photo) {
photo.set('user', wine.user);
state.get('list').add(photo);
wine.publicFirebase.child(photo.get('timestamp')).set(photo.toJSON());
wine.userFirebase.child(photo.get('timestamp')).set(photo.toJSON());
});
wine.photos.on("remove", function(photo) {
state.get('list').removeByIndex(state.get('idx'));
wine.publicFirebase.child(String(photo.get('timestamp'))).remove();
wine.userFirebase.child(String(photo.get('timestamp'))).remove();
});
wine.userFirebase.on("child_added", function(child) {
if (child.user === wine.user && !state.get('list').exists(child.timestamp)) {
var photo = wine.models.Photo(child);
state.get('list').add(photo);
}
});
wine.userFirebase.on("child_removed", function(child) {
if (child.user === wine.user) {
state.get('list').removeByTimestamp(child.get(timestamp));
}
});
},
preRenderViews: function() {
state.set('list', new wine.views.List());
state.get('list').render();
forge.logging.log('Pre-rendered wine list');
state.set('map', new wine.views.Map());
state.get('map').render();
forge.logging.log('Pre-rendered map');
},
disclosure_indicator: function(el) {
forge.tools.getURL('img/disclosure_indicator.png', function(src) {
$('img.icon', el).attr('src', src);
});
},
handleRatingClick: function(rating, el) {
$('label', el).removeClass('no_star');
$('label', el).each(function(idx, el) {
if (parseInt($(el).attr('class').split('_')[1]) > rating) {
$(el).addClass('no_star');
}
});
},
getLocation: function(coords, timestamp) {
forge.request.ajax({
url: "http://maps.googleapis.com/maps/api/geocode/json?latlng="+coords.latitude+","+coords.longitude+"&sensor=true",
dataType: "json",
success: function(response) {
try {
var photo = wine.photos.filter(function(item) {
return item.get('timestamp') == timestamp;
})[0];
if (photo) {
photo.set('location', response.results[0].formatted_address);
Expand Down Expand Up @@ -198,5 +194,4 @@ var wine = {
});
});
}
}
};

0 comments on commit 093fa03

Please sign in to comment.