Skip to content

Instantly share code, notes, and snippets.

@tkh44
Created October 17, 2013 16:37
Show Gist options
  • Select an option

  • Save tkh44/7028112 to your computer and use it in GitHub Desktop.

Select an option

Save tkh44/7028112 to your computer and use it in GitHub Desktop.
var tacoApp = angular.module('tacoApp', ['TacoModel', 'hmTouchevents']);
// Index: http://localhost/views/taco/index.html
tacoApp.controller('IndexCtrl', function ($scope, TacoRestangular) {
// This will be populated with Restangular
$scope.tacos = [];
// Helper function for opening new webviews
$scope.open = function(id) {
webView = new steroids.views.WebView("/views/taco/show.html?id="+id);
steroids.layers.push(webView);
};
// Helper function for loading taco data with spinner
$scope.loadTacos = function() {
$scope.loading = true;
tacos.getList().then(function(data) {
$scope.tacos = data;
$scope.loading = false;
});
};
// Fetch all objects from the backend (see app/models/taco.js)
var tacos = TacoRestangular.all('taco');
$scope.loadTacos();
// Get notified when an another webview modifies the data and reload
window.addEventListener("message", function(event) {
// reload data on message with reload status
if (event.data.status === "reload") {
$scope.loadTacos();
};
});
// -- Native navigation
// Set navigation bar..
steroids.view.navigationBar.show("Taco index");
// ..and add a button to it
var addButton = new steroids.buttons.NavigationBarButton();
addButton.title = "Add";
// ..set callback for tap action
addButton.onTap = function() {
var addView = new steroids.views.WebView("/views/taco/new.html");
steroids.modal.show(addView);
};
// and finally put it to navigation bar
steroids.view.navigationBar.setButtons({
right: [addButton]
});
});
// Show: http://localhost/views/taco/show.html?id=<id>
tacoApp.controller('ShowCtrl', function ($scope, TacoRestangular) {
// Helper function for loading taco data with spinner
$scope.loadTaco = function() {
$scope.loading = true;
taco.get().then(function(data) {
$scope.taco = data;
$scope.loading = false;
});
};
// Save current taco id to localStorage (edit.html gets it from there)
localStorage.setItem("currentTacoId", steroids.view.params.id);
var taco = TacoRestangular.one("taco", steroids.view.params.id);
$scope.loadTaco()
// When the data is modified in the edit.html, get notified and update (edit is on top of this view)
window.addEventListener("message", function(event) {
if (event.data.status === "reload") {
$scope.loadTaco()
};
});
// -- Native navigation
steroids.view.navigationBar.show("Taco: " + steroids.view.params.id );
var editButton = new steroids.buttons.NavigationBarButton();
editButton.title = "Edit";
editButton.onTap = function() {
webView = new steroids.views.WebView("/views/taco/edit.html");
steroids.modal.show(webView);
}
steroids.view.navigationBar.setButtons({
right: [editButton]
});
});
// New: http://localhost/views/taco/new.html
tacoApp.controller('NewCtrl', function ($scope, TacoRestangular) {
$scope.close = function() {
steroids.modal.hide();
};
$scope.create = function(taco) {
$scope.loading = true;
TacoRestangular.all('taco').post(taco).then(function() {
// Notify the index.html to reload
var msg = { status: 'reload' };
window.postMessage(msg, "*");
$scope.close();
$scope.loading = false;
}, function() {
$scope.loading = false;
alert("Error when creating the object, is Restangular configured correctly, are the permissions set correctly?");
});
}
$scope.taco = {};
});
// Edit: http://localhost/views/taco/edit.html
tacoApp.controller('EditCtrl', function ($scope, TacoRestangular) {
var id = localStorage.getItem("currentTacoId"),
taco = TacoRestangular.one("taco", id);
$scope.close = function() {
steroids.modal.hide();
};
$scope.update = function(taco) {
$scope.loading = true;
taco.put().then(function() {
// Notify the show.html to reload data
var msg = { status: "reload" };
window.postMessage(msg, "*");
$scope.close();
$scope.loading = false;
}, function() {
$scope.loading = false;
alert("Error when editing the object, is Restangular configured correctly, are the permissions set correctly?");
});
};
// Helper function for loading taco data with spinner
$scope.loadTaco = function() {
$scope.loading = true;
// Fetch a single object from the backend (see app/models/taco.js)
taco.get().then(function(data) {
$scope.taco = data;
$scope.loading = false;
});
};
$scope.loadTaco();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment