Skip to content

Commit

Permalink
added data plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
DerWaldschrat committed Feb 2, 2013
1 parent 1bb4bb4 commit 0a46e17
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 0 deletions.
117 changes: 117 additions & 0 deletions js/app/data/data.js
@@ -0,0 +1,117 @@
steal("js/app/data/items.js").then(function () {
var ROUTE = "data"
App.router.route(ROUTE, ROUTE, function () {
App.setView(new Abi.View.Data())
})

Abi.Model.Data = Backbone.Model.extend({
urlRoot: "Data/",
idAttribute: "dataid"
})
// We need the delaySave again
_.extend(Abi.Model.Data.prototype, Abi.Mixin.DelaySave)

Abi.Collection.Datas = Backbone.Collection.extend({
model: Abi.Model.Data,
urlRoot: "Data/",
byC: function (c) {
return this._categories[c]
},
// Overwrite add to create a map of the categories
add: function (model, options) {
var ret = Backbone.Collection.prototype.add.call(this, model, options)
var models = this.models
this._categories || (this._categories = {})
for (var i = 0, len = models.length, curr; i < len; ++i) {
curr = models[i]
this._categories[curr.get("categoryid")] = curr
}
return ret
},
// Overwrite reset to reset categories hash
reset: function (a,b) {
this._categories = {}
return Backbone.Collection.prototype.reset.call(this, a, b)
}
}, {
instance: Backbone.Singleton()
})

// Used to beautify our value
function parseValue(val) {
return val.replace(",", ".").replace(/[^0-9.]+/g, "").replace()
}

Abi.View.Data = Backbone.View.extend({
events: {
"change input": "save"
},
initialize: function () {
this.collection = Abi.Collection.Datas.instance({
reset: this.replaceBody
}, this)
},
templateHead: function () {
return "<h1>Ein paar Daten zu dir</h1>"
+ "<div class='alert alert-info'>" +
"<h4>Beachte:</h4>" +
" Diese Daten benötigen wir lediglich, um die Kategorien Minimal-, Maximal- und Durchschnittsabiturient(in) zu erstellen, sie werden also selbstverständlich" +
" nicht unter deinem Namen einfach so veröffentlich!" +
"</div>" +
"<div id='dataBody'></div>"
},
templateForm: function () {
var html = "<form action='#' class=''>" +
"<fieldset>" +
"<legend>Die Daten</legend>"
for (var i = 0, len = ITEMS.length, curr, item, value; i < len; ++i) {
curr = ITEMS[i]
item = this.collection.byC(curr.category)
value = item ? item.escape("value") : ""
html += this.templateItem(curr, value)
}
html += "</fieldset>" +
"</form>"
return html
},
templateItem: function (item, value) {
var title = item.name + (item.unit ? " in " + item.unit : "")
, html = "<label>" +
"<input type='text' value='" + value + "' data-category='" + item.category + "' title='" + title + "' maxlength='15' /> " +
title +
"</label>"
return html
},
render: function () {
this.$el.html(this.templateHead())
return this
},
replaceBody: function () {
this.$("#dataBody").html(this.templateForm())
},
save: function (event) {
var $target = $(event.currentTarget)
, category = $target.attr("data-category")
, model = this.collection.byC(category) || new Abi.Model.Data()
if (model.isNew()) {
model.set("categoryid", category)
this.listenTo(model, "sync", this.addModel)
.listenTo(model, "error", this.removeListen)
.listenTo(model, "clearSave", this.removeListen)
}
var value = parseValue($target.val())
$target.val(value)
model.delaySave(1000, {
value: value
})
},
removeListen: function (model) {
this.stopListening(model)
this.collection.add(model)
},
addModel: function (model) {
this.removeListen(model)
}
})

})
78 changes: 78 additions & 0 deletions js/app/data/items.js
@@ -0,0 +1,78 @@
(function () {
// A list of all datas we want to collect
var ITEMS = window.ITEMS = [{
category: 1,
name: "Größe",
unit: "cm"
}, {
category: 2,
name: "Gewicht",
unit: "Kilogramm"
}, {
category: 3,
name: "Anzahl Geschwister"
}, {
category: 4,
name: "Zigaretten pro Tag"
}, {
category: 5,
name: "Jungfrau (1 = ja, 0 = nein)"
}, {
category: 6,
name: "Bierkonsum pro Woche",
unit: "l"
}, {
category: 7,
name: "Kaffekonsum pro Tag",
unit: "l"
}, {
category: 8,
name: "Schlaf pro Nacht",
unit: "h"
}, {
category: 9,
name: "TV-Konsum pro Tag",
unit: "h"
}, {
category: 10,
name: "Online pro Tag",
unit: "h"
}, {
category: 11,
name: "Fehlstunden pro Woche",
unit: "h"
}, {
category: 12,
name: "Zuspätkommen pro Woche",
unit: "min"
}, {
category: 13,
name: "Verweise"
}, {
category: 14,
name: "Arbeit an der Seminararbeit",
unit: "h"
}, {
category: 15,
name: "Beginn Seminararbeit",
unit: "Monaten vor Abgabe"
}, {
category: 16,
name: "Schuhgröße"
}, {
category: 17,
name: "Taschengeld pro Monat",
unit: "€"
}]


ITEMS._map = {}
// Create map of items
for (var i = 0, len = ITEMS.length; i < len; ++i) {
ITEMS._map[ITEMS[i].category] = ITEMS[i]
}
ITEMS.get = function (c) {
return ITEMS._map[c]
}

})()

0 comments on commit 0a46e17

Please sign in to comment.