September 30, 2012
September 20, 2012
PHP MVC Part - I
MVC:
Model
View
Controller
What is MVC?
MVC is a way of coding. how do we distribute functions/classes/files.
What MVC is not ?
MVC is not any programming language, rather it is a framework/architecture/design schema.
MVC architecture, we can implement in PHP, JAVA, C# and also JavaScript
Analogy:
1. How do you keep things in your kitchen. ?
2. We can keep all tools and utilities scattered on one table - ( No Pattern )
3. We can keep tools categorized by use. i.e. knife,spoon in one box, Plates near sink - ( some pattern )
Real world example:
1. We have to build Book Store
2. We need to add/remove books
3. Show users list of books
4. Registered user can see preview
Here, we have some logic, lets call them, business logic:
i.e. add/remove books:
Also, we have some-thing to show user
i.e. List of books
Also, we have one rule
i.e. Registered user can see preview
All classes/functions of business logic, we can call: MODEL
All classes/files which helps to render product list, we can call:VIEW
All classes/files/functions which helps to decide/implement rule, we can call: CONTROLLER
Technically:
Model: Encompasses business logic ( Mostly base classes )
View: Helps to present/render data ( Mostly template files + classes which helps to render )
Controller: Helps to decide some rule. like, which page to render, which page not to render
Prime Example of MVC architecture:
Zend
Magento - way to complex to understand
End of part one.
Cheers!!
Model
View
Controller
What is MVC?
MVC is a way of coding. how do we distribute functions/classes/files.
What MVC is not ?
MVC is not any programming language, rather it is a framework/architecture/design schema.
MVC architecture, we can implement in PHP, JAVA, C# and also JavaScript
Analogy:
1. How do you keep things in your kitchen. ?
2. We can keep all tools and utilities scattered on one table - ( No Pattern )
3. We can keep tools categorized by use. i.e. knife,spoon in one box, Plates near sink - ( some pattern )
Real world example:
1. We have to build Book Store
2. We need to add/remove books
3. Show users list of books
4. Registered user can see preview
Here, we have some logic, lets call them, business logic:
i.e. add/remove books:
Also, we have some-thing to show user
i.e. List of books
Also, we have one rule
i.e. Registered user can see preview
All classes/functions of business logic, we can call: MODEL
All classes/files which helps to render product list, we can call:VIEW
All classes/files/functions which helps to decide/implement rule, we can call: CONTROLLER
Technically:
Model: Encompasses business logic ( Mostly base classes )
View: Helps to present/render data ( Mostly template files + classes which helps to render )
Controller: Helps to decide some rule. like, which page to render, which page not to render
Prime Example of MVC architecture:
Zend
Magento - way to complex to understand
End of part one.
Cheers!!
September 14, 2012
PHP MVC Singleton pattern
What is Singleton pattern ?
- A simple design schema, in which, we instantiate object only once in a page life cycle.
Explanation with analogy.
- Lets say, you went to restaurant and ordered 3 types of Soup.
- Each Soup comes up with Bowl + Spoon
- Now, you have 3 spoons.
- Normal pattern : you use 3 spoons for each bowl
- problem in that: wastage of 3 spoons
-Singleton pattern: each time, you use: same spoon to drink soup.
- Thats it.
Real-time example:
- DB Object.
- We invoke DB object in page life cycle only once.
- Each time, we need to run query, we use same object.
Example with code:
}
Seems so easy :)
cheers!!!
- A simple design schema, in which, we instantiate object only once in a page life cycle.
Explanation with analogy.
- Lets say, you went to restaurant and ordered 3 types of Soup.
- Each Soup comes up with Bowl + Spoon
- Now, you have 3 spoons.
- Normal pattern : you use 3 spoons for each bowl
- problem in that: wastage of 3 spoons
-Singleton pattern: each time, you use: same spoon to drink soup.
- Thats it.
Real-time example:
- DB Object.
- We invoke DB object in page life cycle only once.
- Each time, we need to run query, we use same object.
Example with code:
class db{
publis static $_instance;
public static _getInstance(){
// Lets check, do we have object already instantiated ?
if(self::$_instance){
// return already instantiated object
return self::$_instance
}
else{
// else instantiate and return object
return self::$instance = new db();
}
}
}
Seems so easy :)
cheers!!!
September 12, 2012
Javascript Closure
Good way in javascript to have dynamic content using closure
Lets say, we have to use the string:
" content has been posted 1 times"
" content has been posted 2 times"
we can use below code
Cheers!!
Lets say, we have to use the string:
" content has been posted 1 times"
" content has been posted 2 times"
we can use below code
var message = function(count){ return " content has been posted " + count + times "; } console.log(message(1)); console.log(message(2));
Cheers!!
September 11, 2012
jQuery $.ajax IE and Cross Domain
jQuery $.ajax fails in IE for cross-domain request, even-though we have allowed origin on server configuration.
Basically AJAX is an transport mechanism, with few rules. we can have our own transport too. Here is how we can override $.ajax transport and allow cross-domain AJAX request in IE.
Add below code at the end of page.
Basically AJAX is an transport mechanism, with few rules. we can have our own transport too. Here is how we can override $.ajax transport and allow cross-domain AJAX request in IE.
Add below code at the end of page.
$.ajaxTransport("+*", function( options, originalOptions, jqXHR ) { if(jQuery.browser.msie && window.XDomainRequest) { var xdr; return { send: function( headers, completeCallback ) { // Use Microsoft XDR xdr = new XDomainRequest(); xdr.open("get", options.url); xdr.onload = function() { if(this.contentType.match(/\/xml/)){ var dom = new ActiveXObject("Microsoft.XMLDOM"); dom.async = false; dom.loadXML(this.responseText); completeCallback(200, "success", [dom]); }else{ completeCallback(200, "success", [this.responseText]); } }; xdr.ontimeout = function(){ completeCallback(408, "error", ["The request timed out."]); }; xdr.onerror = function(){ completeCallback(404, "error", ["The requested resource could not be found."]); }; xdr.send(); }, abort: function() { if(xdr)xdr.abort(); } }; } });
Cheers!!
Subscribe to:
Posts (Atom)