var StoryView = ScrollableView.extend({ loaded: false, el: "#story", template: $("#story .template").html(), events: { "click .links li": "pick", }, initialize: function(){ this.sections = {} this.$img = this.$("img") this.$content = this.$(".content") this.$links = this.$(".links") this.$loader = this.$(".loader") this.scroller = new IScroll('#story', app.iscroll_options) }, show: function(){ this.deferScrollToTop() app.footer.hide() document.body.className = "story" }, populate: function(data){ if (this.loaded) { console.warn("populate called twice"); return } this.loaded = true this.data = data this.$loader.hide() this.$content.empty() // id title image[uri caption] body this.data.forEach(function(row){ var t = this.template.replace(/{{id}}/, row.id) .replace(/{{body}}/, row.body.replace(/\n/g, "
")) var li = document.createElement("li") li.dataset.id = row.id li.innerHTML = row.title this.sections[row.id] = row this.$links.append(li) this.$content.append(t) }.bind(this)) this.set_active( this.data[0].id ) }, pick: function(e){ var id = e.currentTarget.dataset.id this.set_active(id) }, set_active: function(id){ this.$links.find(".active").removeClass("active") this.$links.find("[data-id=" + id + "]").addClass("active") this.$content.find(".active").removeClass("active") this.$content.find("[data-id=" + id + "]").addClass("active") var section = this.sections[id] this.$img.attr("src", section.image.uri) this.deferScrollToTop() }, })