Ability to dynamically update URL.
parent
5437afa3eb
commit
b52d1cf154
20
Readme.md
20
Readme.md
|
@ -140,7 +140,7 @@ You should specify an image URL via data-media attribute on `<li class="pinteres
|
|||
|
||||
### Manual initialization
|
||||
|
||||
Could be useful on dynamic (Ajax) websites.
|
||||
Could be useful on dynamic (AJAX) websites.
|
||||
|
||||
```html
|
||||
<ul id="share">
|
||||
|
@ -153,6 +153,24 @@ Could be useful on dynamic (Ajax) websites.
|
|||
$('#share').socialLikes();
|
||||
```
|
||||
|
||||
### Dynamic URL changing
|
||||
|
||||
You can dynamically replace URL and title without reinitialization.
|
||||
|
||||
```html
|
||||
<ul id="share2" class="social-likes" data-url="http://example.com/" data-title="My example">
|
||||
<li class="facebook">Facebook</li>
|
||||
...
|
||||
</ul>
|
||||
```
|
||||
|
||||
```javascript
|
||||
$('#share2').socialLikes({
|
||||
url: 'http://github.com/',
|
||||
title: 'GitHub'
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
### Events
|
||||
|
||||
|
|
|
@ -275,13 +275,20 @@ section {
|
|||
<li class="facebook" title="Опубликовать ссылку на Фейсбуке">Facebook</li>
|
||||
<li class="twitter" data-via="sapegin" data-related="DessiTeckel:Моя такса" title="Опубликовать ссылку в Твиттере">Twitter</li>
|
||||
</ul>
|
||||
|
||||
<p><a href="#" id="social-likes-update">Change URL</a></p>
|
||||
<script>
|
||||
var sl = $('#social-likes-manual');
|
||||
sl.on('counter.social-likes', function(event, service, number) {
|
||||
console.log('Counter', service, number);
|
||||
});
|
||||
sl.socialLikes();
|
||||
$('#social-likes-update').click(function() {
|
||||
sl.socialLikes({
|
||||
url: 'https://github.com/sapegin/grunt-webfont/',
|
||||
title: 'SVG to webfont converter for Grunt'
|
||||
});
|
||||
return false;
|
||||
});
|
||||
</script>
|
||||
|
||||
<h3>Manual initialization with JavaScript options</h3>
|
||||
|
|
|
@ -189,7 +189,16 @@ var counters = {
|
|||
$.fn.socialLikes = function(options) {
|
||||
return this.each(function() {
|
||||
var elem = $(this);
|
||||
new SocialLikes(elem, $.extend({}, $.fn.socialLikes.defaults, options, dataToOptions(elem)));
|
||||
var instance = elem.data(prefix);
|
||||
if (instance) {
|
||||
if ($.isPlainObject(options)) {
|
||||
instance.update(options);
|
||||
}
|
||||
}
|
||||
else {
|
||||
instance = new SocialLikes(elem, $.extend({}, $.fn.socialLikes.defaults, options, dataToOptions(elem)));
|
||||
elem.data(prefix, instance);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -225,8 +234,9 @@ SocialLikes.prototype = {
|
|||
|
||||
this.makeSingleButton();
|
||||
|
||||
this.buttons = [];
|
||||
buttons.each($.proxy(function(idx, elem) {
|
||||
new Button($(elem), this.options);
|
||||
this.buttons.push(new Button($(elem), this.options));
|
||||
}, this));
|
||||
|
||||
if (this.options.counters) {
|
||||
|
@ -281,6 +291,20 @@ SocialLikes.prototype = {
|
|||
|
||||
this.widget = widget;
|
||||
},
|
||||
update: function(options) {
|
||||
if (options.url === this.options.url) return;
|
||||
|
||||
// Reset counters
|
||||
this.number = 0;
|
||||
this.countersLeft = this.buttons.length;
|
||||
if (this.widget) this.widget.find('.' + prefix + '__counter').remove();
|
||||
|
||||
// Update options
|
||||
$.extend(this.options, options);
|
||||
for (var buttonIdx = 0; buttonIdx < this.buttons.length; buttonIdx++) {
|
||||
this.buttons[buttonIdx].update(options);
|
||||
}
|
||||
},
|
||||
updateCounter: function(e, service, number) {
|
||||
if (number) {
|
||||
this.number += number;
|
||||
|
@ -325,17 +349,13 @@ Button.prototype = {
|
|||
init: function() {
|
||||
this.detectParams();
|
||||
this.initHtml();
|
||||
this.initCounter();
|
||||
},
|
||||
|
||||
if (this.options.counters) {
|
||||
if (this.options.counterNumber) {
|
||||
this.updateCounter(this.options.counterNumber);
|
||||
}
|
||||
else {
|
||||
var extraOptions = this.options.counterUrl ? { counterUrl: this.options.counterUrl } : {};
|
||||
counters.fetch(this.service, this.options.url, extraOptions)
|
||||
.always($.proxy(this.updateCounter, this));
|
||||
}
|
||||
}
|
||||
update: function(options) {
|
||||
$.extend(this.options, options);
|
||||
this.widget.find('.' + prefix + '__counter').remove(); // Remove old counter
|
||||
this.initCounter();
|
||||
},
|
||||
|
||||
detectService: function() {
|
||||
|
@ -416,6 +436,19 @@ Button.prototype = {
|
|||
this.button = button;
|
||||
},
|
||||
|
||||
initCounter: function() {
|
||||
if (this.options.counters) {
|
||||
if (this.options.counterNumber) {
|
||||
this.updateCounter(this.options.counterNumber);
|
||||
}
|
||||
else {
|
||||
var extraOptions = this.options.counterUrl ? { counterUrl: this.options.counterUrl } : {};
|
||||
counters.fetch(this.service, this.options.url, extraOptions)
|
||||
.always($.proxy(this.updateCounter, this));
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
cloneDataAttrs: function(source, destination) {
|
||||
var data = source.data();
|
||||
for (var key in data) {
|
||||
|
|
Loading…
Reference in New Issue