-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.sensei.renderer.js
70 lines (57 loc) · 2.36 KB
/
jquery.sensei.renderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
(function($){
$.SenseiUI.Renderer = function(el, options){
// To avoid scope issues, use 'base' instead of 'this'
// to reference this class from internal events and functions.
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
// Add a reverse reference to the DOM object
base.$el.data("SenseiUI.Renderer", base);
base.init = function(){
base.options = $.extend({},$.SenseiUI.Renderer.defaultOptions, options);
// Put your initialization code here
base.$el.bind("SenseiUI.search.searching", function(e) {
base.$el.css({opacity: 0.5});
});
base.$el.bind("SenseiUI.search.success", function(e, data) {
base.options.setupContainer(base.$el);
$(data.results).each( function (i, item) {
var r = base.options.format(item);
r.appendTo(base.$el);
});
base.$el.css({opacity: 1});
base.options.afterRender(base.$el);
});
base.$el.bind("SenseiUI.search.failure", function(e) {
base.$el.css({opacity: 1});
});
};
// Sample Function, Uncomment to use
// base.functionName = function(paramaters){
//
// };
// Run initializer
base.init();
};
$.SenseiUI.Renderer.defaultOptions = {
format: function(item){
return $("<div></div>")
.addClass("result")
.append( $("<a></a>").attr("href", item.link || item.url ).text(item.title || item.name) )
.append( $("<span></span>").addClass("description").html(item.snippet_text || item.text) );
},
setupContainer: function($el){
$el.html("");
},
afterRender: function($el) {
// do nothing. You may want to re arrange items,
// append some sort of legend, zebra items, you name it.
}
};
$.fn.SenseiRenderer = function(options){
return this.each(function(){
(new $.SenseiUI.Renderer(this, options));
});
};
})(jQuery);