/*global View Application unescape */ // A basic view implementation that expects an `URL` to be passed as one of the parameters // to the view; it will format that `URL` using any other parameters, and use it to retrieve // an `HTML` fragment from the server. This is only one of many strategies that could be // followed to create views, of course. It also assumes that the parameters object will // get a title property. window.BaseView = View.extend({ // Subclasses must ensure there is an url property to the parameter object passed // into this superconstructor, i.e.: // // this._super(params) // // Any parameters passed in to the view constructor will be substituted // into the URL string using the String format method (see utilities.js). // // @param {Object} params initialize: function(params) { this.url = this.getUrl(params); this.params = params; }, getUrl: function(params) { if (params.url) { return params.url.format(params); } else { return "/"+Application.params.serverPath+"/" + params.view; } }, getTitle: function() { // The title varies on the Team View between Android and iOS. // cstuart 2011-08-29 if (Application.client.ios) { this.params.title = this.params.ios_title || this.params.title; } return this.params.title || this.params.view.capitalize(); }, getMetric: function() { return this.params.metric || "/" + this.params.view.capitalize(); }, setMetric: function(metric) { console.log('((( setMetric '+ metric +' )))'); return this.params.metric = metric; }, getButtons: function() { return this.params.buttons || "Back:"; }, // This provides support for the NFL v5 menu implementation. // Possibly NFL could subclass the BaseView getV5MenuButton: function() { return this.params.v5MenuButton || "showMainMenu"; }, getAction: function() { return this.params.action || ""; }, getFilter: function() { return this.params.filter || ""; }, getSection: function() { return this.params.section || this.params.view; }, shouldRefreshAd: function() { return this.params.hasOwnProperty('refreshAd') ? this.params.refreshAd : true; }, // Loads the content of the view from the server (the content should be wrapped by a `div`). // The content is inserted into the page. create: function(params) { if (!params.skipLoadingIndicator) { $.showLoader(); } // This is a hack. The TSN keys include periods that confuse the Rails page caching system. Convert // here to underscores, and back to periods again on the server using a filter. this.url = this.url.dotToBar(); $.ajax({ url: this.url, dataType: 'html', success: $.proxy(function(response) { if (params.isAutoUpdating && params.view !== Application.currentView.params.view) { console.log('$$ BaseView.create(): Not appending autoupdate view. We are no longer on that view!'); } else { // NBA3 only // if ($.highResifyLogosForRetinaAndAndroid) { // response = $.highResifyLogosForRetinaAndAndroid(response); // } // Get a node rather than plain HTML this.element = Application.buffer.html(response).find('div').get(0); Application.appendView(this); // NBA3 only // if ($.delayedLoad) { // $.delayedLoad(); // } } }, this), error: function() { $.hideLoader(); $.flash("A network error occurred.", Application.currentView.params, true); throw new Error('An error occurred.'); } }); }, onClientCallback: function(name, value) { console.log('BaseView.onClientCallback({' + name + ': ' + value + '})'); }, // If HTML returned from the server includes a `
` element, // this should contain an object literal (JSON string) with values that will be merged into // the parameters for this view instance. This is used to pass back additional information // from the server via the HTML. For example, when loading a player it might be useful to // return the key for the player's team so that can be used when communicating with the client: // // // // This will add the integer 1588 (correctly typed) under the property "team_key" to the // parameters (`this.params`) for this view. _evaluateDataAttribute: function() { var params = $('#html_params', Application.wrapper).data('params'); if (params) { var data = JSON.parse(params); for (var key in data) { if (data.hasOwnProperty(key)) { // If you escape a non-string value, it is first converted to a string, which we don't want this.params[key] = (typeof data[key] === "string") ? unescape(data[key]) : data[key]; } } } } });