There is few ways to create javascript object and use its as class.
In this artical i will you my favorite way to implement javascript classes.
I will combaine jQuery in this example.
First of all lets create basic object:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | var GarryObj = { init: function(options, target) { this.options = $.extends({}, this.options, options); this.$target = $(target); this._draw(); }, options: { width: 100, height: 100, }, _draw: function() { this.$target.css({width: this.options.width, height: this.options.height}); } } |
We create here basic class that have 2 functions and one object to store the options.
the init class gets user options and the target (like html div).
then we merge the default options with the user options using jQuery extends function and
store the target as jQuery instance as $target.
The _draw function manipulate the target – here we build the ui by user options.
To create an instance of the class we need to add some code to the (i use to put it on js heaer file).
1 2 3 4 5 6 7 | if (typeof Object.create !== 'function') { Object.create = function (o) { function F() {} F.prototype = o; return new F(); }; } |
Now lets create a instance of our class:
We need to add target div:
and than
1 2 | var garryTest = Object.create(GarryObj); garryTest.init({width: 150, height: 150}, $("#test_div")); |
Have fun ![]()
Garry Lachman
Develop PHP Facebook Application Locally – Dev Mode
Hi,
As you know Facebook doesn’t have develop mode to that allow you to work locally.
But there is way to do it – and its very easy to deployment.
First create 2 applications on Facebook, one for “dev” and one for “production”.
In dev application set you application url to the locally localhost if “127.0.0.1″ like:
http://127.0.0.1/your_app.
The second step is to create two “environments” in your application – “dev mode” and “production mode”.
Create a settings class that check if you work locally and return the Facebook API Key.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class ApplicationSettings {
private static $facebook_api = array(
"dev"=>array(
"key"=>"123",
"secret"=>"123"
),
"prod"=>array(
"key"=>"123",
"secret"=>"123"
)
);
public static function get_facebook() {
if ($_SERVER['HTTP_HOST'] == "127.0.0.1") {
return self::$facebook_api["dev"];
}
return self::$facebook_api["prod"];
}
}
?>
Set your Facebook API Key and Secret to the “dev” and “prod” modes.
Now we will change the way we connection to facebook api to get the
settings from our new class.
2
3
4
5
6
7
8
$facebook_settings = ApplicationSettings::get_facebook();
$facebook = new Facebook(array(
'appId' => $facebook_settings["key"],
'secret' => $facebook_settings["secret"],
));
?>
Now every time we connect to Facebook API the settings class will
check our domain and if we work on “127.0.0.1″ its automatically
gets the “dev mode” API that pointed to our local application.
Have Fun
Garry Lachman