Releasing CI Models in javacript! [ajax proxy] |
[eluser]GreatSlovakia[/eluser]
Imagine you could directly call your models right from your javascript. Just think a few seconds what that would mean... thought about it? In ColdFusion there is native functionality for this and now I build it into codeigniter as well ![]() EXAMPLE Let's first explore how it looks like: A fairly normal model, except for the new "__JS" method. This method defines that the model can be used from javascript (the allow some more customization which I will explain later on) exampleModel.php: Code: <?php Next there is some configuration stuff I will skip, so let's look at the most simplistic controller possible: exampleController.php Code: <?php And that's it, now if you would enter into the console exampleModel you will get back a javascript object with the same functions and functionality as the original php model: ![]() Code: data=exampleModel.sql(); INSTALLATION Lets get to the installation stuff, first add the MY_Loader.php to the core directory (copy it from here). Next either create a new controller or add a method to an existing controller containing Code: $this->load->ajax_proxy(); Code: /* ![]() CUSTOMIZATION THE __JS() FUNCTION Exclude specific methods from being accessed from javascript Code: function __JS(){ Code: function __JS(){ THE ajax_model() FUNCTION Just like with the normal model function it is possible to specify a second parameter containing the name which the model should use, e.g. Code: public function index(){ Tips, Tricks & Security with the __JS() function Exposing models to the outside world might be quite dangerous to do, however using the __JS function you can protect your model whilst keeping all your checks outside the functions themselves. Take for example Code: function __js(){ FAQ Why did I place this inside an Loader.php extend? Despite the fact that this could just as well be used as a stand alone library (just a few small changes would be necessary), I created it like this because I am currently working on implementing a number of functions from coldfusion in codeigniter and I would like these functions to be part of the core, as that's unlikely to happen anytime soon (due to the way the impregnable guidelines which protect codeigniter) I want to at least treat and design it as much as core stuff as possible. Secondly I am still trying to build a neat solution which would make "ajax" a reserved controller name and bypass the need of the ajax_proxy() function. I got quite close to this by building a hellish MY_Router.php file, but it was so ugly that I didn't dare to release it ![]() So what does this exactly do? Look at the image which you doubtlessly missed, as ExpresionEngine seems to not support img tags or something :S ![]() How does this work? When you call one of the javascript functions it will JSON.stringify the arguments and call the ajax proxy controller method which in turn will call the model for your, which will json_encode() the return value for you which will be returned by the javascript function to your script. However, in essence, you probably should worry about that and simply use it. The only thing you might need to be aware of is that you should only return arrays or simple types (integers, strings, etc.) inside your model, although objects will sometimes work as well. ... why is there a 6000 character limit :S... |
Messages In This Thread |
Releasing CI Models in javacript! [ajax proxy] - by El Forum - 06-13-2011, 11:46 AM
Releasing CI Models in javacript! [ajax proxy] - by El Forum - 06-14-2011, 09:33 AM
Releasing CI Models in javacript! [ajax proxy] - by El Forum - 06-17-2011, 04:04 PM
Releasing CI Models in javacript! [ajax proxy] - by El Forum - 06-18-2011, 10:18 AM
Releasing CI Models in javacript! [ajax proxy] - by El Forum - 06-20-2011, 12:44 PM
Releasing CI Models in javacript! [ajax proxy] - by El Forum - 06-20-2011, 02:24 PM
|