tyedye/public/js/app.js

107 lines
2.9 KiB
JavaScript
Raw Normal View History

2014-08-11 19:48:22 +02:00
// Model definition
var Stat = Backbone.Model.extend({
2014-08-12 01:37:55 +02:00
urlRoot: '/tyedye/stat/',
2014-08-11 19:48:22 +02:00
defaults: {
name: 'New Stat',
value: 0,
default_value: 0,
},
initialize: function () {
this.on("change", function(model) {});
}
});
var Stats = Backbone.Collection.extend({
model: Stat,
2014-08-12 01:37:55 +02:00
url: function () {
if (this.generic) {
return "/tyedye/stats/";
} else {
return "/tyedye/stats/" + this.id;
}
},
initialize: function (models, player_id) {
this.generic = true;
if (typeof player_id != 'undefined') {
this.id = player_id;
this.generic = false;
}
this.fetch({ reset: true });
if (!this.generic) {
this.on('reset', function () {
this.each(function (statistic) {
$("#player_stats" + this.id )
.append('<label>' + statistic.name + '</label><input type="text" name="' + statistic.name + this.id + '">');
});
});
}
2014-08-11 19:48:22 +02:00
}
});
var Player = Backbone.Model.extend({
urlRoot: '/tyedye/player/',
defaults: {
name: 'New Player',
stats: new Stats,
2014-08-12 01:37:55 +02:00
code: 0,
2014-08-11 19:48:22 +02:00
},
initialize: function () {
2014-08-12 01:37:55 +02:00
// this.on("change", function (model) {});
if (this.get('code') != 0) {
var player_stats = new Stats([], this.get('code'));
this.set('stats', player_stats);
}
}
2014-08-11 19:48:22 +02:00
});
var Players = Backbone.Collection.extend({
model: Player,
url: "/tyedye/players/",
initialize: function () {
2014-08-12 01:37:55 +02:00
this.fetch({ reset: true });
2014-08-11 19:48:22 +02:00
}
});
var stats = new Stats;
var players = new Players;
// Routers
var MainRouter = Backbone.Router.extend({
routes: {
"" : "index",
"player/:player_id" : "player_detail",
},
index: function () { ;; },
player_detail: function () { ;; }
});
var main_router = MainRouter;
// History controller
Backbone.history.start({root: '/tyedye'});
// Views definition
var PlayerListView = Backbone.View.extend({
initialize: function () {
2014-08-12 01:37:55 +02:00
this.listenTo(this.collection, 'reset', this.render);
2014-08-11 19:48:22 +02:00
},
collection: players,
render: function () {
// Load and compile the template
2014-08-12 01:37:55 +02:00
var template = _.template( $("#player_list_template").html(), {});
2014-08-11 19:48:22 +02:00
// Put the compiled html into our 'el' element, putting in on the webpage
this.$el.html(template);
2014-08-12 01:37:55 +02:00
this.collection.each(function (playerItem) {
$("div#player_ul_list").append('<h3 style="font-size: large;"><a href="/tyedye/#/player/' + playerItem.get('code') + '/">' + playerItem.get('name') + '</a></h3><div id="player_stats' + playerItem.get('code') + '" style="display: none;"></div>');
});
$("#player_number").text(this.collection.length);
}
});
2014-08-11 19:48:22 +02:00
var player_list_view = new PlayerListView({ el: $("#player_list") });