/static/js/views/stackplacesview.js
JavaScript | 109 lines | 76 code | 21 blank | 12 comment | 6 complexity | e212051b481bc1cecea1c2dadd126a68 MD5 | raw file
Possible License(s): Apache-2.0
- define([
- 'config',
- 'underscore',
- 'backbone',
- 'lib/template',
-
- 'views/listview',
- 'views/placeitemview',
- 'logger'
- ], function (
- config,
- util,
- Backbone,
- template,
-
- ListView,
- PlaceItemView,
- logger
- ) {
- var __ListView = ListView.prototype;
- var __PlacesView = {
- _listRendered: false,
- className: 'stackplaces',
- template: template('<ul class="placelist"></ul>'),
- attachPoints: {
- 'list': '.placelist',
- 'items': '.placelist > li > a[data-itemid]'
- },
- view: PlaceItemView,
- initialize: function (options) {
- if (!this.model) throw new Error(
- 'StackPlacesView requires a StackModel'
- );
- if (!this.collection) throw new Error(
- 'StackPlacesView requires a PlaceCollection'
- );
- options = options || {};
- util.defaults(options, {
- target: '.placelist'
- });
-
- var attachPoints = this.attachPoints || {};
- // initial rendering
- $(this.el).html(this.template( this.model.toJSON() ));
- // Run superconstructor to bind events.
- __ListView.initialize.call(this, options);
- var render = this.render;
-
- // TODO: should we be doing this logic in render()?
- // TODO: should we abstract this cool granular update code?
- this.model.bind('change', function(model){
- var changedProperties = model.changedAttributes(),
- selector = null;
- for(var key in changedProperties) {
- selector = this.attachPoints[key];
- if(selector) {
- this.$(selector).html(changedProperties[key]);
- }
- }
- }, this);
- var collection = this.collection;
- collection
- // TODO: determine when the stack fires `add`. It should
- // only do so when we're adding new places, not when we're
- // adding multiple sites from the server side.
- .bind('add', render, this)
- .bind('reset', render, this)
- .bind('remove', render, this);
- // When sites are visited, update the stack model.
- // TODO: if this is primarily how we use selectPlace, it should probably
- // just live on the StackModel. -GB
- this.bind('visit', function (view, model, e) {
- e.preventDefault();
- var collection = this.collection,
- place_id = model.id;
- // Select the place in this stack
- collection
- .selectPlace(place_id)
- .changePlace(place_id);
- // Issue a warp request to Lattice.
- collection.stack().warp(place_id);
- }, this);
- },
-
- render: function(){
- __ListView.render.apply(this, arguments);
- this._listRendered = true;
- return this;
- }
- };
- var PlacesView = ListView.extend(__PlacesView);
-
- return PlacesView;
- });