Created
May 14, 2014 08:20
-
-
Save omissis/fa646e8e477223d94703 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($, _, MyApp) { | |
"use strict"; | |
$.ns("MyApp.Notification"); | |
MyApp.Notification.FlashMessages = (function () { | |
var defaultOptions = { | |
cookieName: 'flashes', | |
templateSelector: '#flash-message-template', | |
containerSelector: '#flash-messages' | |
}; | |
var options = {}; | |
return { | |
init: function (opts) { | |
options = $.extend({}, defaultOptions, opts); | |
var flashes = $.cookie(options.cookieName); | |
if ($.isVoid(flashes)) { | |
return; | |
} | |
this.render({ flashes: flashes }); | |
$.removeCookie(options.cookieName); | |
}, | |
render: function (attributes) { | |
attributes = attributes || {}; | |
if ($.isVoid(attributes.flashes)) { | |
return; | |
} | |
var flashMessageTemplate = $(options.templateSelector).html(); | |
var $flashMessages = $(options.containerSelector); | |
try { | |
var flashes = JSON.parse(attributes.flashes); | |
for (var i in flashes) { | |
if (!flashes.hasOwnProperty(i)) { | |
continue; | |
} | |
for (var j = 0, len = flashes[i].length; j < len; j += 1) { | |
$flashMessages.append(_.template(flashMessageTemplate, { | |
type: i, | |
message: flashes[i][j] | |
})); | |
} | |
} | |
} catch (e) { | |
// json syntax error | |
} | |
} | |
}; | |
}()); | |
}(jQuery, _, MyApp)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment