2016-03-27 19:50:07 +08:00
|
|
|
/* global Metamaps, $, Hogan, Backbone */
|
2016-02-03 21:38:41 +08:00
|
|
|
|
2016-03-27 19:50:07 +08:00
|
|
|
/*
|
|
|
|
* Metamaps.Views.js.erb
|
|
|
|
*
|
|
|
|
* Dependencies:
|
|
|
|
* - Metamaps.Famous
|
|
|
|
* - Metamaps.Loading
|
|
|
|
*/
|
2016-02-03 21:38:41 +08:00
|
|
|
|
2016-03-27 19:50:07 +08:00
|
|
|
Metamaps.Views = {
|
|
|
|
initialized: false
|
|
|
|
}
|
2016-02-03 21:38:41 +08:00
|
|
|
|
2016-03-27 19:50:07 +08:00
|
|
|
Metamaps.Views.init = function () {
|
|
|
|
Metamaps.Views.MapperCard = Backbone.View.extend({
|
|
|
|
template: Hogan.compile($('#mapperCardTemplate').html()),
|
|
|
|
|
2016-08-11 01:44:40 +00:00
|
|
|
tagName: 'div',
|
2016-03-27 19:50:07 +08:00
|
|
|
|
|
|
|
className: 'mapper',
|
|
|
|
|
|
|
|
render: function () {
|
|
|
|
this.$el.html(this.template.render(this.model))
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
Metamaps.Views.MapCard = Backbone.View.extend({
|
|
|
|
template: Hogan.compile($('#mapCardTemplate').html()),
|
|
|
|
|
|
|
|
tagName: 'div',
|
|
|
|
|
|
|
|
className: 'map',
|
|
|
|
|
|
|
|
id: function () {
|
|
|
|
return this.model.id
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function () {
|
|
|
|
this.listenTo(this.model, 'change', this.render)
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function () {
|
|
|
|
this.$el.html(this.template.render(this.model.attrForCards()))
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
var MapsWrapper = Backbone.View.extend({
|
|
|
|
initialize: function (opts) {},
|
|
|
|
setCollection: function (collection) {
|
|
|
|
if (this.collection) this.stopListening(this.collection)
|
|
|
|
this.collection = collection
|
|
|
|
this.listenTo(this.collection, 'add', this.render)
|
|
|
|
this.listenTo(this.collection, 'successOnFetch', this.handleSuccess)
|
|
|
|
this.listenTo(this.collection, 'errorOnFetch', this.handleError)
|
|
|
|
},
|
|
|
|
render: function (mapperObj, cbArg) {
|
|
|
|
var that = this
|
|
|
|
|
|
|
|
if (typeof mapperObj === 'function') {
|
|
|
|
var cb = mapperObj
|
|
|
|
mapperObj = null
|
|
|
|
}
|
|
|
|
|
|
|
|
this.el.innerHTML = ''
|
|
|
|
|
|
|
|
// in case it is a page where we have to display the mapper card
|
|
|
|
if (mapperObj) {
|
|
|
|
var view = new Metamaps.Views.MapperCard({ model: mapperObj })
|
|
|
|
|
|
|
|
that.el.appendChild(view.render().el)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.collection.each(function (map) {
|
|
|
|
var view = new Metamaps.Views.MapCard({ model: map })
|
|
|
|
|
|
|
|
that.el.appendChild(view.render().el)
|
|
|
|
})
|
|
|
|
this.$el.append('<div class="clearfloat"></div>')
|
2016-08-01 13:38:57 -04:00
|
|
|
$('#exploreMaps').empty().html(this.el)
|
|
|
|
if (cb) cb()
|
2016-03-27 19:50:07 +08:00
|
|
|
Metamaps.Loading.hide()
|
|
|
|
},
|
|
|
|
handleSuccess: function (cb) {
|
|
|
|
if (this.collection && this.collection.id === 'mapper') {
|
|
|
|
this.fetchUserThenRender(cb)
|
|
|
|
} else {
|
|
|
|
this.render(cb)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
handleError: function () {
|
|
|
|
console.log('error loading maps!') // TODO
|
|
|
|
},
|
|
|
|
fetchUserThenRender: function (cb) {
|
|
|
|
var that = this
|
|
|
|
// first load the mapper object and then call the render function
|
|
|
|
$.ajax({
|
|
|
|
url: '/users/' + this.collection.mapperId + '/details.json',
|
|
|
|
success: function (response) {
|
|
|
|
that.render(response, cb)
|
2016-02-03 21:38:41 +08:00
|
|
|
},
|
2016-03-27 19:50:07 +08:00
|
|
|
error: function () {
|
|
|
|
that.render(cb)
|
2016-02-03 21:38:41 +08:00
|
|
|
}
|
2016-03-27 19:50:07 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2016-02-03 21:38:41 +08:00
|
|
|
|
2016-03-27 19:50:07 +08:00
|
|
|
Metamaps.Views.exploreMaps = new MapsWrapper()
|
|
|
|
}
|