Commits

Dmitri Lebedev  committed 73477b2

refactoring the js

  • Participants
  • Parent commits 0de71c9

Comments (0)

Files changed (1)

File example/media/js/partial.js

 	self.default_selector = '.partial_update'; // blocks that are updated by default
 	self.ignore_selector = '.partial_ignore';  // anchors that should not be captured by this module
 	self.xhr_selector = '.partial_xhr'; // anchors that do not change History.State but do make partial request and insert data in the DOM
+	
+	self.block_selector = '.partial_block'
+	self.update_parent_selector = '.partial_parent'
+	
 	var History = window.History,
 		root_url = History.getRootUrl(),
-		State = History.getState();
+		State = History.getState(),
+		jcurrent_anchor;
 	
 	self.data = {};
 	
 	// other functions can also be overridden
 	
+	self.set_data = function (data) {
+		$.extend(self.data, data);
+	};
+	
 	self.replace_all_blocks = function (data, status, xhr) {
 		// replaces the innerHTML of the existing elements with the blocks from the response
 		// the target elements must have id="<block_name>"
 		$.each(data.blocks, function (key, val) {
 			var jnode = $('#' + key);
-			jnode.html(val)
-			jnode.trigger('partial_update');
-			self.install_handlers(jnode);
+			jnode.html(val).trigger('partial_update', [jnode, data.data]);
 		});
-		self.send_data_event(data.data);
-	};
-	
-	self.send_data_event = function(data) {
-		$(document).trigger('partial_data_update', data);
-		self.data = data;
+		self.set_data(data.data);
 	};
 	
 	self.replace_page_content = function (data, status, xhr) {
 		// explicitly replaces only content block and title
 		var blocks = data.blocks;
-		self.replace_all_blocks({blocks: {content: blocks.content, title: blocks.title}, data: data.data});
+		self.replace_all_blocks({blocks: blocks, data: data.data});
 	};
 	
 	self.on_resource_error = function (xhr, status, error) {
 		$('#content').text('There was an error. Try reloading the page later');
 	};
 
-	self.get_resource = function (ajax_settings, janchor) {
+	self.get_resource = function (ajax_settings) {
 		var new_settings = $.extend({
 			url: State.url,
 			success: self.replace_page_content,
 		
 		try {
 			$.ajax(new_settings);
-			if (janchor) {
-				janchor.trigger('partial_post_request');
+			if (jcurrent_anchor) {
+				jcurrent_anchor.trigger('partial_post_request');
 			}
 		} catch (a) { } // suppress xhr errors. Not sure if this really works.
 	};
 	if (History.enabled) {
 		History.Adapter.bind(window, 'statechange', function() {
 			var State = History.getState();
-			janchor = State.data.janchor;
 			State.data.janchor = undefined;
-			self.get_resource({url: State.url, data: State.data}, janchor);
+			self.get_resource({url: State.url, data: State.data});
 		});
 	};
 	
 	};
 	
 	self.update_internal_link = function (event) {
-		var janchor = $(this),
-			url = janchor.attr('href');
+		jcurrent_anchor = $(this);
+		var url = jcurrent_anchor.attr('href');
 		
 		if (self.is_internal_link(url)) { // this check is here, because it makes no sense to check every link in the page on load. Only few of them will be clicked anyway.
-			janchor.trigger('partial_pre_request');
-			var blocks_ids = ($(this).attr('partial-blocks') || $(self.default_selector)).map(function (i, v) { return v.id; }).get().join(',');
-		
-			History.pushState({blocks: blocks_ids, janchor: janchor}, null, url);
+			jcurrent_anchor.trigger('partial_pre_request');
+			var blocks_ids = (jcurrent_anchor.attr('partial-blocks') || $(self.default_selector)).map(
+				function (i, v) { return v.id; }).get().join(',');
 			
+			History.pushState({blocks: blocks_ids}, null, url);
 			return false;
 		}
 	};
 			return false;
 		}
 	};
-
+	
 	// installing the event handlers
-	self.install_handlers = function (context) {
-		var janchors = $('*[href]', $(context || window.document));
+	self.install_handlers = function (event, jnode) {
+		var janchors = $('*[href]', jnode);
 		if (History.enabled) {
 			janchors.not(self.ignore_selector).not(self.xhr_selector).click(self.update_internal_link);
 		}
 		janchors.filter(self.xhr_selector).click(self.update_partial_as_is);
-	}
+	};
 
 	/* decorated in a function to avoid an infinite loop in jQuery */
-	$(function() { self.install_handlers(); }); // executed on document.ready
+	$(function() {
+		$(document).bind('partial_update', self.install_handlers).trigger('partial_update', [$(document)]);
+	}); // executed on document.ready
 });