Skip to content

Commit

Permalink
Return instance when calling remodal()
Browse files Browse the repository at this point in the history
  • Loading branch information
vodkabears committed Mar 5, 2014
1 parent 6ee93b5 commit 90e2996
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
19 changes: 17 additions & 2 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ <h1>Remodal</h1>
<a class="remodal-cancel" href="#">Cancel</a>
<a class="remodal-confirm" href="#">OK</a>
</div>

<div data-remodal-id="modal2">
<h1>Another one window</h1>
<p>
Hello!
</p>
<br>
<a class="remodal-confirm" href="#">Hello!</a>
</div>
</div>


Expand Down Expand Up @@ -70,8 +79,14 @@ <h1>Remodal</h1>
console.log('cancel');
});

var inst = $.remodal.lookup[$('[data-remodal-id=modal]').data('remodal')];
inst.open();
// You can open or close it like this:
// var inst = $.remodal.lookup[$('[data-remodal-id=modal]').data('remodal')];
// inst.open();
// inst.close();

// Or init in this way:
var inst = $('[data-remodal-id=modal2]').remodal();
// inst.open();
</script>
</body>
</html>
14 changes: 11 additions & 3 deletions src/jquery.remodal.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,21 @@

if ($) {
$["fn"][pluginName] = function (opts) {
return this["each"](function (i, e) {
var instance;
this["each"](function (i, e) {
var $e = $(e);
if (!$e.data(pluginName)) {
var instance = new Remodal($e, opts);
instance = new Remodal($e, opts);
$e.data(pluginName, instance.index);

if (instance.settings.hashTracking &&
$e.attr("data-" + pluginName + "-id") === location.hash.substr(1)) {
instance.open();
}
}
});

return instance;
};
}

Expand Down Expand Up @@ -289,6 +297,7 @@

if ($elem.length) {
var instance = $[pluginName].lookup[$elem.data(pluginName)];

if (instance && instance.settings.hashTracking) {
instance.open();
}
Expand All @@ -297,5 +306,4 @@
}
};
$(window).bind("hashchange." + pluginName, hashHandler);
hashHandler(null, false);
})(window["jQuery"] || window["Zepto"]);

0 comments on commit 90e2996

Please sign in to comment.