// 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. var 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; this.buffer = Application.params.BUFFER_EL; }, getUrl: function(params) { if (params.url) { return params.url.format(params); } else { return "/" + Application.params.APP_NAME + "/" + params.view; } }, getTitle: function() { // The title varies on the Team View between Android and iOS. // cstuart 2011-08-29 if ($.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(); }, getButtons: function() { return this.params.buttons || "Back:"; }, getAction: function() { return this.params.action || ""; }, getFilter: function() { return this.params.filter || ""; }, getSection: function() { return this.params.view; }, // 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() { var view = this; // 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. view.url = view.url.replace(/\./g,"_"); function onError() { var params = {}; // The old currentView won't exist in the case of a 404 or // other server error. if (Application.currentView && Application.currentView.params) { params = Application.currentView.params; } $.hideLoader(); $.flash("A network error occurred.", params, true); throw new Error('An error occurred.'); } $.showLoader(); $.ajax({ url: view.url, success: function(response) { // If the server is unreachable, zepto calls `success` // and then the `response` is empty. Catch it here and throw an error. if (response === "") onError(); var response = $.highResifyLogosForRetinaAndAndroid(response); view.buffer.html(response); view.element = view.buffer.find('div').get(0); view._evaluateDataAttribute(); Application.appendView(view); $.hideLoader(); $.delayedLoad(); }, error: function() { onError(); } }); }, 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 view_data_el = Application.params.BUFFER_EL.find("#view_data"); if (view_data_el.size() === 0) return; if (!view_data_el.attr('data')) return; var data = eval.call(window, "(" + view_data_el.attr("data") + ")"); for (var key in data) { // 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]; } } });