Permalink
Browse files

refactoring

  • Loading branch information...
1 parent 6477a22 commit c9f335c2359ace3cbcb5bfa537398c2dc111226c @pklingem committed Feb 1, 2012
Showing with 52 additions and 35 deletions.
  1. +28 −19 livelist.coffee
  2. +23 −15 livelist.js
  3. +1 −1 livelist.min.js
View
@@ -51,23 +51,28 @@ class window.List extends Utilities
fetch: (options) ->
@fetchRequest.abort() if @fetchRequest
searchTerm = @search.searchTerm()
- params = @filters.setPresets(options.presets)
+ params = {}
+ params.filters = @filters.setPresets(options.presets)
+
+ if searchTerm
+ params.q = searchTerm
+ if options.page
+ params.page = options.page
- if searchTerm then params.q = searchTerm
- if options.page then params.page = options.page
@fetchRequest = $.ajax(
- url : @urlPrefix
- dataType : 'json'
- data : params
- type : @httpMethod
- beforeSend : @displayFetchingIndication
- success : @renderIndex
+ url : @urlPrefix
+ dataType : 'json'
+ data : params
+ type : @httpMethod
+ beforeSend : @displayFetchingIndication
+ success : @renderIndex
)
render: ->
partials = {}
partials[@resourceNameSingular] = @listItemTemplate
- $(@renderTo).html( Mustache.to_html(@listTemplate, @data, partials) )
+ listHTML = Mustache.to_html(@listTemplate, @data, partials)
+ $(@renderTo).html( listHTML )
@removeFetchingIndication()
window.LiveList.version = '0.0.3'
@@ -76,11 +81,15 @@ class window.Filters extends Utilities
constructor: (globalOptions, options = {}) ->
@setOptions(globalOptions)
@filters = if options.presets then _.keys(options.presets) else []
- unless @filters.cookieName then @filters.cookieName = 'livelist_filter_presets'
+ @initializeCookies()
@setOptions(options)
$('input.filter_option', @renderTo).live( 'change', => $(@listSelector).trigger(@eventName) )
$(@advancedOptionsToggleSelector).click(@handleAdvancedOptionsClick)
+ initializeCookies: ->
+ if jQuery.cookie && @useCookies && @cookieName
+ @cookieName = 'livelist_filter_presets'
+
presets: ->
cookie = jQuery.cookie(@cookieName) if jQuery.cookie && @useCookies
if @useCookies && cookie
@@ -89,15 +98,13 @@ class window.Filters extends Utilities
@presets
setPresets: (presets) ->
- params = { filters: {} }
-
+ filters = {}
if jQuery.isEmptyObject(presets)
- params.filters = @selections()
+ filters = @selections()
@setCookie() if jQuery.cookie
else
- params.filters = presets
-
- params
+ filters = presets
+ filters
setCookie: (params_filters) ->
if not jQuery.isEmptyObject(params_filters)
@@ -146,7 +153,8 @@ class window.Filters extends Utilities
render: (data) ->
@filters = _.pluck( data.filters, 'filter_slug' )
- $(@renderTo).html( Mustache.to_html(@template, data) )
+ filtersHTML = Mustache.to_html(@template, data)
+ $(@renderTo).html( filtersHTML )
if @noFiltersSelected(data) && data[@resourceName].length > 0
$('input[type="checkbox"]', @renderTo).attr('checked', 'checked')
@@ -217,7 +225,8 @@ class window.Pagination extends Utilities
render: (data) ->
@pagination = @paginationJSON(data.pagination)
- $(@renderTo).html( Mustache.to_html(@paginationTemplate, @pagination) )
+ paginationHTML = Mustache.to_html(@paginationTemplate, @pagination)
+ $(@renderTo).html( paginationHTML )
handlePaginationLinkClick: (event) =>
event.preventDefault()
View
@@ -98,7 +98,8 @@
var params, searchTerm;
if (this.fetchRequest) this.fetchRequest.abort();
searchTerm = this.search.searchTerm();
- params = this.filters.setPresets(options.presets);
+ params = {};
+ params.filters = this.filters.setPresets(options.presets);
if (searchTerm) params.q = searchTerm;
if (options.page) params.page = options.page;
return this.fetchRequest = $.ajax({
@@ -112,10 +113,11 @@
};
List.prototype.render = function() {
- var partials;
+ var listHTML, partials;
partials = {};
partials[this.resourceNameSingular] = this.listItemTemplate;
- $(this.renderTo).html(Mustache.to_html(this.listTemplate, this.data, partials));
+ listHTML = Mustache.to_html(this.listTemplate, this.data, partials);
+ $(this.renderTo).html(listHTML);
return this.removeFetchingIndication();
};
@@ -135,16 +137,20 @@
this.handleAdvancedOptionsClick = __bind(this.handleAdvancedOptionsClick, this);
this.setOptions(globalOptions);
this.filters = options.presets ? _.keys(options.presets) : [];
- if (!this.filters.cookieName) {
- this.filters.cookieName = 'livelist_filter_presets';
- }
+ this.initializeCookies();
this.setOptions(options);
$('input.filter_option', this.renderTo).live('change', function() {
return $(_this.listSelector).trigger(_this.eventName);
});
$(this.advancedOptionsToggleSelector).click(this.handleAdvancedOptionsClick);
}
+ Filters.prototype.initializeCookies = function() {
+ if (jQuery.cookie && this.useCookies && this.cookieName) {
+ return this.cookieName = 'livelist_filter_presets';
+ }
+ };
+
Filters.prototype.presets = function() {
var cookie;
if (jQuery.cookie && this.useCookies) {
@@ -158,17 +164,15 @@
};
Filters.prototype.setPresets = function(presets) {
- var params;
- params = {
- filters: {}
- };
+ var filters;
+ filters = {};
if (jQuery.isEmptyObject(presets)) {
- params.filters = this.selections();
+ filters = this.selections();
if (jQuery.cookie) this.setCookie();
} else {
- params.filters = presets;
+ filters = presets;
}
- return params;
+ return filters;
};
Filters.prototype.setCookie = function(params_filters) {
@@ -198,8 +202,10 @@
};
Filters.prototype.render = function(data) {
+ var filtersHTML;
this.filters = _.pluck(data.filters, 'filter_slug');
- $(this.renderTo).html(Mustache.to_html(this.template, data));
+ filtersHTML = Mustache.to_html(this.template, data);
+ $(this.renderTo).html(filtersHTML);
if (this.noFiltersSelected(data) && data[this.resourceName].length > 0) {
return $('input[type="checkbox"]', this.renderTo).attr('checked', 'checked');
}
@@ -264,8 +270,10 @@
};
Pagination.prototype.render = function(data) {
+ var paginationHTML;
this.pagination = this.paginationJSON(data.pagination);
- return $(this.renderTo).html(Mustache.to_html(this.paginationTemplate, this.pagination));
+ paginationHTML = Mustache.to_html(this.paginationTemplate, this.pagination);
+ return $(this.renderTo).html(paginationHTML);
};
Pagination.prototype.handlePaginationLinkClick = function(event) {
Oops, something went wrong. Retry.

0 comments on commit c9f335c

Please sign in to comment.