Modernizr.load Deprecated. Yepnope.js Deprecated. Current Alternatives? - deprecated

Modernizr.load and yepnope.js have both been deprecated.
How do we conditionally call javascript files now?
Could you give me a example?
This is the javascript that I want to load
var BubbleShoot = window.BubbleShoot || {};
BubbleShoot.Game = (function($){
var Game = function(){
this.init = function(){
$(".but_satart_game").bind("click", startGame);
};
var startGame = function(){
console.log("dentro de startGame en game.js");
//$(".but_satart_game").unbind("click");
//BubbleShoot.ui.hideDialog();
};
};
return Game;
})(jQuery);
And modernizr's code:
Modernizr.load({
load: "_js/game.js",
complete: function(){
$(function(){
var game = new BubbleShoot.Game();
game.init();
})
});

You can dynamically add scripts to a page with document.createElement and adding it to the DOM with .async = true and call your game's init() function from the script's load event-listener:
function addGameScriptToPage() {
const scriptElement = document.createElement('script');
scriptElement.async = true;
scriptElement.addEventListener( 'load', function( e ) { new BubbleShoot.Game().init(); } );
scriptElement.src = '__js/game.js';
document.head.appendChild( scriptElement );
}
You can make it more generic by passing the script's URL as a parameter and returning a Promise for the load event-listener, so pages using this function can have their own custom load logic:
function addScriptToPage( scriptUrl ) {
return new Promise( ( resolve, reject ) => {
const scriptElement = document.createElement('script');
scriptElement.async = true;
scriptElement.addEventListener( 'load', function( e ) {
resolve( e );
);
scriptElement.addEventListener( 'error', function( e ) {
reject( e );
);
scriptElement.src = scriptUrl;
document.head.appendChild( scriptElement );
} );
}
Used like so:
async function doStuff() {
const shouldLoadGame = prompt( "Would you like to play a game of global thermonuclear war?" );
if( shouldLoadGame ) {
try {
await addScriptToPage( "__js/game.js" );
// This code runs when the 'load' event listener calls `resolve(e)`.
const g = new BubbleShoot.Game();
g.init();
}
catch( err ) {
// This code runs when the 'error' event listener calls `reject(e)`.
alert( "Game failed to load." ); //
}
}
}
...and this is pretty much how the require() function for loading modules on-demand works, btw.

Related

FullCalendar V4 How to set attribute of calendar event after drop

I'm using FullCalendar V4 callback function drop. I try to pass myID which are generated by server to be Calendar event.id, but I do not know how to do. The following is simple code
var calendar = new Calendar(calendarEl, {
drop: function( dropInfo ) {
$.getJSON( url, myParams, function( data ) {
// try to set data.myID to FullCalendar event id
calendarEvent = { id : data.myID };
});
},
eventClick: function( eventClickInfo ) {
var msg = "Are you sure to delete[" + event.title + "]?";
if ( confirm(msg) ) {
// It fails, because I can't get event.id
var params = { method: "deleteEvent", id: event.id };
$.get(url, params, function( data ){
eventClickInfo.event.remove();
});
}
}
});
I have tried to putcalendar.refetchEvents(); into the drop: $.getJSON(function(){}) response block, but FullCalendar makes 2 event in UI. One has balnk attribute, the other has right attribute. If I can eliminate the redundancy, it will be a good solution.
Thanks for ADyson's suggestion. Finally, I used callback function eventReceive solving the problem. The following is my simple code
var calendar = new Calendar(calendarEl, {
eventReceive: function( info ) {
var $draggedEl = $( info.draggedEl );
var params = { method: "insert" };
$.ajaxSetup({ cache: false, async: false});
$.getJSON( myURL, params, function( data ){ // insert information into DB
info.event.setProp( "id", data.id );
});
},
eventClick: function( info ) {
var event = info.event;
var msg = "Are you sure to delete[" + event.title + "]?";
if ( confirm(msg) ) {
var params = { method: "deleteEvent", id: event._def.id };
$.get( myURL, params, function( data ){
event.remove();
});
}
},
});

Meteor getting values from Async function

I am having trouble with getting return value from wrapAsync function in meteor. Below is my code
Template.greet.helpers({
greet : function () {
var convertAsyncToSync = Meteor.wrapAsync( HTTP.get );
resultOfAsyncToSync = convertAsyncToSync('http://www.demo.com/api/greet', {} );
console.log(resultOfAsyncToSync);
return resultOfAsyncToSync;
}
});
I get undefined value in console.
try
Template.greet.onCreated(function(){
this.apiResult = new ReactiveVar(null);
})
Template.greet.helpers({
greet : function () {
var t = Template.instance();
HTTP.get('http://www.demo.com/api/greet', {}, function(e,r){
//process response and save it in reactivevar
t.apiResult.set(r.data);
});
return t.apiResult.get();
}
});
don't forget to add package meteor add reactive-var
EDIT
Template.greet.onRendered(function(){
var t = Template.instance();
HTTP.get('http://www.demo.com/api/greet', {}, function(e,r){
//process response and save it in reactivevar
t.apiResult.set(r.data);
});
})
Template.greet.helpers({
greet : function () {
var t = Template.instance();
return t.apiResult.get();
}
});

Update dynamic data in service-worker.js

I have the below data coming in form of array from a url.
[{"title":"hey hi","body":"hello","url":"https://simple-push-demo.appspot.com/","tag":"new"}]
service-worker.js
it has the above url in fetch()
'use strict';
console.log('Started', self);
self.addEventListener('install', function(event) {
self.skipWaiting();
console.log('Installed new', event);
});
self.addEventListener('activate', function(event) {
console.log('Activatednew', event);
});
self.addEventListener('push', function(event) {
try{
console.log('Push message', event);
var ev = event;
//sample
return fetch("http://localhost/push-notifications-master/app/json.php").then(function(ev,response) {
response = JSON.parse(JSON.stringify(response));
return response;
}).then(function(ev,j) {
// Yay, `j` is a JavaScript object
console.log("j", j);
for(var i in j) {
var _title = j[i].title;
var _body = j[i].body;
var _tag = j[i].tag;
console.log("_body", _body);
}
ev.waitUntil(
self.registration.showNotification("push title", {
body: _body,
icon: 'images/icon.png',
tag: _tag
}));
});
return Promise.all(response);
}
catch(e){console.log("e", e)}
});
I am trying to see the above array data coming from that particular url in console.log("j",j);. but it shows undefined. How can i get dymanic data in sw.js Please Guide.
In your addEventListener('push' .... method, I think it might be better to wait for a response before parsing it.
Also, to be checked, but your php request should be in https (not checked by myself, but my request are on https).
Here how I do this :
event.waitUntil(
fetch('YOUR PHP URL').then(function(response) {
if (response.status !== 200) {
console.log('Problem. Status Code: ' + response.status);
throw new Error();
}
// Examine the text in the response
return response.json().then(function(data) {
if (data.error || !data.notification) {
console.error('The API returned an error.', data.error);
throw new Error();
}
var title = data.notification[0].title;
var body = data.notification[0].body;
var icon = data.notification[0].icon;
var notificationTag = data.notification[0].tag;
return self.registration.showNotification(title, {body: body,icon:icon, tag: notificationTag});
});
})
);
The json :
{"notification" : [{"title":"TITLE","body":"BODY","icon":"URL TO ICON","tag":"TAG"}]}
Hope it can be useful.

Node.js TCP Client command/response

I have a simple server, you send it a command, it replies back with an \r\n delimited response.
So I tried to get a command( callback ) method on my client. Check out this simplified code snippet:
var net = require('net');
var Client = function() {
this.data = "";
this.stream = net.createConnection(port, host);
this.stream.on('data', function( data ) {
var self = this;
this.data += data;
self.process()
};
this.process = function() {
var _terminator = /^([^\r\n]*\r\n)/;
while( results = _terminator.exec(this.data) ) {
var line = results[1];
this.data = this.data.slice(line.length);
this.emit('response', data);
};
};
this.sendCommand = function( command, callback ) {
var self = this;
var handler = function( data ) {
self.removeListener('response', handler);
callback && callback(data);
}
this.addListener('response', handler);
this.stream.write(command);
};
this.command_1 = function( callback ) {
this.sendCommand( 'test', callback );
};
this.command_2 = function( callback ) {
this.sendCommand( 'test2', callback );
};
}
So I am doing a client.command_1( function() {} ) and then a client.command_2( function() {}) but in the callback of my command_2 I am getting the response from command_1.
Is this the right way to implement such a thing?
When you execute
client.command_1( function() { 1; } );
client.command_2( function() { 2; } );
you add both callbacks as 'result' listeners, and when emit('result') happens for the first time, both callback are called (then first callback removes itself from a list). You need to set callbacks on some kind of request object, not on a client.
simple code on what happens in your client:
var e = new EventEmitter();
e.on('result', function() { console.log(1); });
e.on('result', function() { console.log(2); });
// ...
e.emit('result'); // here we trigger both callbacks which result in printing "1\n2\n"

Changing the value of a Telerik RadEditor with Javascript/jQuery

I'm trying to manually clean the HTML of a Telerik RadEditor with Javascript but I can't seem to find the correct place to store the value so that it gets saved on post back.
Here's the JS I have:
$(function () {
jQuery.fixHash = function ($html) {
// modify $html
return $html;
};
$("#adminEditingArea input[id$='SaveButton']").unbind("click").click(function () {
$("iframe[id$='_contentIframe']").trigger("save");
// call .net postback
return false;
});
});
var editorSaveEventInit = false;
function InitSaveEvent() {
if (!editorSaveEventInit) {
var $EditFrames = $("iframe[id$='_contentIframe']");
if ($EditFrames && $EditFrames.length > 0) {
$EditFrames.bind("save", function (e) {
var $thisFrame = $(this);
var thisFrameContents = $thisFrame.contents();
if (thisFrameContents) {
var telerikContentIFrame = thisFrameContents.get(0);
var $body = $("body", telerikContentIFrame);
var html = $.fixHash($body).html();
$body.html(html);
// also tried storing the modified HTML in the textarea, but it doesn't seem to save:
//$thisFrame.prev("textarea").html(encodeURIComponent("<body>" + html + "</body>"));
}
});
editorSaveEventInit = true;
}
}
};
$(window).load(function () {
InitSaveEvent();
});
Is there any way to access the Telerik RadEditor object with JavaScript (using OnClientCommandExecuted()?) so that I can access the .get_html() and .set_html(value) functions? If not, what values do I need to set before posting back?
Why don't you use custom content filters.
Ah, just discovered Telerik's built-in $find() function: http://www.telerik.com/help/aspnet-ajax/editor_getingreferencetoradeditor.html
Edit: here's the solution I came up with for my InitSaveEvent() function:
var editorSaveEventInit = false;
function InitSaveEvent() {
if (!editorSaveEventInit) {
var $EditFrames = $("iframe[id$='_contentIframe']");
if ($EditFrames && $EditFrames.length > 0) {
$EditFrames.bind("save", function (e) {
var $thisFrame = $(this);
var thisFrameContents = $thisFrame.contents();
if (thisFrameContents) {
var telerikContentIFrame = thisFrameContents.get(0);
var $body = $("body", telerikContentIFrame);
var html = $.fixHash($body).html();
// SOLUTION!
var $radeditor = $thisFrame.parents("div.RadEditor.Telerik:eq(0)");
var editor = $find($radeditor.attr("id"));
editor.set_html(html);
// ☺
}
});
editorSaveEventInit = true;
}
}
};

Resources