Skip to content

Instantly share code, notes, and snippets.

@mxrguspxrt
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mxrguspxrt/8b9b61c32168b3d304fd to your computer and use it in GitHub Desktop.
Save mxrguspxrt/8b9b61c32168b3d304fd to your computer and use it in GitHub Desktop.
just saving
var Yo = function(){}
Yo.Application = function(){
this.Router = null;
this.Controllers = {};
this.Views = {};
this.Repositories = {};
this.Models = {};
}
Yo.Router = function(routes){
this.routes = routes;
}
Yo.Router.prototype.redirectTo = function(path){
history.pushState({}, path, path);
this.routes[path](); // should be done onto url changes not forcing
}
Yo.Controller = function(actions){
this.__proto__ = actions;
}
Yo.Views = function(){
this.compiledTemplates = {};
}
Yo.Views.prototype.loadTemplate = function(name){
if(!this.compiledTemplates[name]){
var html = $("[data-template='"+name+"']").html();
alert(name);
this.compiledTemplates[name] = Handlebars.compile(html);
}
return this.compiledTemplates[name];
}
Yo.Views.prototype.render = function(name, values){
for(var key in values){
var _this = this;
values[key].notifications.on("load", function(){
var template = _this.loadTemplate(name);
var htmlWithValues = template(values);
$("#content").html(htmlWithValues);
});
}
}
Yo.Notifications = function(){
this.listeners = {};
}
Yo.Notifications.prototype.notify = function(message){
if(!this.listeners[message]){
return false;
}
for(k in this.listeners[message]){
this.listeners[message][k]();
}
}
Yo.Notifications.prototype.on = function(message, listener){
if(!this.listeners[message]){
this.listeners[message] = [];
}
this.listeners[message].push(listener);
}
Yo.Repository = function(opts){
this.dataType = "json";
this.url = opts.url;
}
Yo.Repository.prototype.all = function(){
var result = [];
result.notifications = new Yo.Notifications();
var success = function(data){
$.merge(result, data);
result.notifications.notify("load");
}
$.ajax({type: "GET", url: this.url, dataType: this.dataType, success: success});
return result;
}
Yo.Repository.prototype.find = function(id){
return $.ajax({type: "GET", url: this.url+"/"+id, dataType: this.dataType});
}
Yo.Repository.prototype.create = function(data){
return $.ajax({type: "POST", url: this.url, dataType: this.dataType, data: data});
}
Yo.Repository.prototype.update = function(data){
return $.ajax({type: "PUT", url: this.url+"/"+data.id, dataType: this.dataType, data: data});
}
Yo.Repository.prototype.delete = function(id){
return $.ajax({type: "DELETE", url: this.url+"/"+id, dataType: this.dataType});
}
Yo.Model = function(opts){
this.notifications = new Yo.Notifications();
this.repository = opts.repository;
}
Yo.Model.prototype.all = function(){
return this.repository.all();
}
Yo.Model.prototype.find = function(id){
return this.repository.find(id);
}
Yo.Model.prototype.create = function(id){
return this.repository.create(id);
}
Yo.Model.prototype.update = function(id){
return this.repository.find(id);
}
Yo.Model.prototype.delete = function(id){
return this.repository.delete(id);
}
var App = new Yo.Application();
App.Repositories.Task = new Yo.Repository({url: "/api/tasks"});
App.Repositories.Todo = new Yo.Repository({url: "/api/todos"});
App.Models.Task = new Yo.Model({repository: App.Repositories.Task});
App.Models.Todo = new Yo.Model({repository: App.Repositories.Todo});
App.Views = new Yo.Views();
App.Controllers.Tasks = new Yo.Controller({
index: function(){
var tasks = App.Models.Task.all();
App.Views.render("tasks/index", {tasks: tasks});
},
new: function(){
var task = {Headline: "yo"};
task.notifications = new Yo.Notifications();
App.Views.render("tasks/new", {task: task});
task.notifications.notify("load");
},
create: function(params){
var task = new App.Models.Task.create(params);
App.Views.render("tasks/create", {task: task});
},
show: function(params){
var task = App.Models.Task.find(params.id);
App.Views.render("tasks/show", {task: task});
},
edit: function(params){
var task = App.Models.Task.find(params.id);
App.Views.render("tasks/edit", {task: task});
},
update: function(params){
var task = App.Models.Task.find(params.id);
task.update(params);
show();
},
delete: function(params){
var task = App.Models.Task.delete(params.id);
App.Router.redirectTo("/tasks");
}
});
App.Controllers.Todos = new Yo.Controller({
index: function(params){
var task = App.Models.Task.find(params.task_id);
var todos = App.Models.Todo.Where({task_id: params.task_id});
App.Views.render("todos/index", {task: task, todos: todos});
},
new: function(){
var todo = new App.Models.Todo();
App.Views.render("todos/new", {task: task});
},
create: function(params){
var todo = App.Models.Todo.create(params);
App.Views.render("todos/create", {task: task});
},
show: function(params){
var todo = App.Models.Todo.find(params.id);
App.Views.render("todos/show", {task: task});
},
edit: function(params){
var todo = App.Models.Todo.find(params.id);
App.Views.render("todos/edit", {task: task});
},
update: function(params){
var todo = App.Models.Todo.find(params.id);
todo.update(params);
App.Views.render("todos/update", {task: task});
}
});
App.Router = new Yo.Router({
"GET /tasks": App.Controllers.Tasks.index,
"GET /tasks/new": App.Controllers.Tasks.new,
"POST /tasks": App.Controllers.Tasks.create,
"GET /tasks/:id": App.Controllers.Tasks.show,
"PUT /tasks/:id": App.Controllers.Tasks.update,
"PUT /tasks/:id": App.Controllers.Tasks.delete,
"GET /tasks/:task_id/todos": App.Controllers.Todos.index,
"GET /tasks/:task_id/todos/new": App.Controllers.Todos.new,
"POST /tasks/:task_id/todos": App.Controllers.Todos.create,
"GET /tasks/:task_id/todos/:id": App.Controllers.Todos.show,
"PUT /tasks/:task_id/todos/:id": App.Controllers.Todos.update,
"DELETE /tasks/:task_id/todos/:id": App.Controllers.Todos.delete
});
$(document).ready(function(){
App.Controllers.Tasks.new();
});
//App.Models.Task.create({headline: "Yo", description: "Yo", state: "Yo"});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment