This is a blog for me to keep my scripting notes in, if you like the code go ahead and copy it.
Thursday, January 21, 2016
Fractured Nations Common JS Library
After using several Common JS libraries for dependency management I decided that none of them offered me all the things I wanted. I wanted small and fast like Require. I also wanted to load modules like Node and Inject with module.exports. I wanted modules to preload ahead of execution. I also wanted easy path rewrite abilities that Inject offered. And I wanted it easy to understand. So last summer I started building my own.
I give you Fractured Nations Common JS. It is a Common JS dependency management library with Deferred Promises, Ajax and custom logging. The Common JS supports require, module.exports, and define. The Deferred Promises support then, done, fail, always, resolve, and reject to be compliant with Promise/A and the Jquery standard if you are in the mood to ditch Jquery. The Ajax library is similar to Jquery but is not very developed. The logging allows you to turn it on and off and set how much logging you desire for certain parts of the code.
Give it a spin. There are several examples, including a fully working site that has knockout.js enabled.
https://github.com/josephtveter/fn_common
Sunday, October 12, 2014
Alpha Build
I just got my personal framework to Alpha build. It works... mostly but there is not much to it.
The main focus of it is to make fast and easy web applications that scale. It will be the base framework for my web games. If your into web applications and want to check out a cool Common JS framework, I am offering the Alpha Build free on my site.www.fracturednations.com
If you want to see it working, its not much, but it works. Here are two demo sites that I am putting together.
www.fracturednations.info
system.fracturednations.info
Joseph Tveter
www.fracturednations.com
The main focus of it is to make fast and easy web applications that scale. It will be the base framework for my web games. If your into web applications and want to check out a cool Common JS framework, I am offering the Alpha Build free on my site.www.fracturednations.com
If you want to see it working, its not much, but it works. Here are two demo sites that I am putting together.
www.fracturednations.info
system.fracturednations.info
Joseph Tveter
www.fracturednations.com
Wednesday, July 30, 2014
Geolocation woes
Apparently HTML5 has an api to get me exactly where I am on the planet. But it can't tell me what country I am in. Google will tell me. Google knows all.
this.getGeoLocation = function()
{
var deferred = $.Deferred();
if(navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(function(position)
{
deferred.resolve(position);
//position: {"coords":{"speed":null,"accuracy":65,"altitudeAccuracy":10,"altitude":1410.8349609375,"longitude":-111.72789187811416,"heading":null,"latitude":40.332329714635044},"timestamp":1406740874211}
});
}
else
{
deferred.reject({error: "FAIL"});
}
return deferred.promise();
};
this.getGeoLocation = function()
{
var deferred = $.Deferred();
if(navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(function(position)
{
deferred.resolve(position);
//position: {"coords":{"speed":null,"accuracy":65,"altitudeAccuracy":10,"altitude":1410.8349609375,"longitude":-111.72789187811416,"heading":null,"latitude":40.332329714635044},"timestamp":1406740874211}
});
}
else
{
deferred.reject({error: "FAIL"});
}
return deferred.promise();
};
Wednesday, July 2, 2014
Wait for it....
Deferred Objects with jquery. Great for when you are waiting for data to process or servers to reply.
var deferredData = $.Deferred();
deferredData.done(function(result)
{
//do stuff
}).fail(function(errorObj)
{
//handel error
}).always(function(result)
{
//whatever is returned comes here as well.
});
// when the data is done simply call
deferredData.resolve(result);
//or if you want to fail
deferredData.reject(errorObj);
var deferredData = $.Deferred();
deferredData.done(function(result)
{
//do stuff
}).fail(function(errorObj)
{
//handel error
}).always(function(result)
{
//whatever is returned comes here as well.
});
// when the data is done simply call
deferredData.resolve(result);
//or if you want to fail
deferredData.reject(errorObj);
Tuesday, January 7, 2014
Web page scaling to the device!
Web page scaling to the device! Even with different pixel ratios! Will work with android, iPhone and web!
Enjoy!
var dpi = window.devicePixelRatio || 1;
var width = $(window).width();
var ratio = 52; //Ratio of target font size to screen width screenWidth/idealFontSize
var font = Math.ceil((width / dpi / ratio)+dpi*3);
if(font < 15)
font = 15;
// any less is not useable.
$("html").css({"fontSize": font});
Enjoy!
Saturday, November 9, 2013
Cookie Module and Common JS
With the slow death of flash I have decided to build my game on a javascript MVC framework. The core is using the Common JS library Inject.
This mornings project was creating a cookie manager. I stole and modified some basic cookie functions from W3Schools, and put it into the Common JS structure. Happy Scripting.
Here is the code to call it.
////////////////////////////////////////////////////
var Cookie = require("modules.model.Cookie");
var App = function()
{
this.Cookie = new Cookie();
//set the cookie
this.Cookie.setCookie("UserName", "Bob");
//Get the cookie
var userName = this.Cookie.getCookie("UserName");
alert("Username = " + userName);
//is the cookie
var isUser = this.Cookie.isCookieValue("UserName", "Bob");
alert("Is Bob the User? " + isUser);
};
///////////////////////////////////////////////////
Here is the module
/////////////////////////////////////////////////
var Cookie = function()
{
var self = this;
this.DEFAULT_EXP = 30; //days
this.setCookie = function(c_name,value,exdays)
{
var exdate=new Date();
if(!exdays)
{
exdays = self.DEFAULT_EXP;
}
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
};
this.getCookie = function(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
};
this.isCookieValue = function(name, value)
{
debugger;
var cookie = self.getCookie(name);
if(cookie===null || cookie==="")
{
return false;
}
else
{
if(cookie === value)
{
return true;
}
else
{
return false;
}
}
};
};
module.exports = Cookie;
///////////////////////////////////////////////
This mornings project was creating a cookie manager. I stole and modified some basic cookie functions from W3Schools, and put it into the Common JS structure. Happy Scripting.
Here is the code to call it.
////////////////////////////////////////////////////
var Cookie = require("modules.model.Cookie");
var App = function()
{
this.Cookie = new Cookie();
//set the cookie
this.Cookie.setCookie("UserName", "Bob");
//Get the cookie
var userName = this.Cookie.getCookie("UserName");
alert("Username = " + userName);
//is the cookie
var isUser = this.Cookie.isCookieValue("UserName", "Bob");
alert("Is Bob the User? " + isUser);
};
///////////////////////////////////////////////////
Here is the module
/////////////////////////////////////////////////
var Cookie = function()
{
var self = this;
this.DEFAULT_EXP = 30; //days
this.setCookie = function(c_name,value,exdays)
{
var exdate=new Date();
if(!exdays)
{
exdays = self.DEFAULT_EXP;
}
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
};
this.getCookie = function(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
};
this.isCookieValue = function(name, value)
{
debugger;
var cookie = self.getCookie(name);
if(cookie===null || cookie==="")
{
return false;
}
else
{
if(cookie === value)
{
return true;
}
else
{
return false;
}
}
};
};
module.exports = Cookie;
///////////////////////////////////////////////
Saturday, August 17, 2013
Singleton vrs Dependency Injection.
I have been starting to nail down the framework for my game. I have been reviewing my php knowledge base, on how to solve global variable, and object issues. I have friends that have been using singleton classes, and I defiantly see where they are going with it. It is handy for making sure you only have one instance of the thing running around, especially if you are dealing with a single object such as a web user.
But I just don't feel it is the right solution for me. I think I am going to go more toward dependency injection at the bootstrap. My controller structure extends each other based on the page. So the sub page of a sub page will have been extended with each page all the way back to the main site controller. And any additional needed dependencies can be loaded for a new sub page controller. I can see going singleton if you have more then 1 controller object, which I have seen that approach before but it seemed cumbersome. I don't like cumbersome. I think it will be one controller that has been extended from the root, with dependency injection. Seems like it would be cleaner.
Subscribe to:
Posts (Atom)