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: (or look below at the attached file) 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 or check out the attachment below. 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...
[eluser]GreatSlovakia[/eluser]
Hope u liked it at least@benboi. Got to say I am suprised nobody liked it "more". I just mean, when I first heard about ajax proxies in coldfusion my initial reaction was... why in the world would you need "proxies" for that... later on I read through it and once I realised what it actually did I was really excited about it. I mean, it's the most fluent two-way communication I have ever heard about between javascript and php. Would anybody care to explain why they think this isn't great? (and I am not talking about my implementation, but about the idea itself, because fair is fair, all credits go to whoever came up with this first (although the implementation wasn't easy neither ))
[eluser]Sayian[/eluser]
Hello, Tested this out and I'm having problems getting it to work. I followed the above directions for installation. -- In config.php Code: $config['ajax_proxy'] = 'test/index'; -- In Controller "Test.php" Code: class Test extends MY_Controller { -- In Modelname.php Code: class Modelname extends CI_Model { ... Then from another controller named Dashboard Code: class Dashboard extends Account_Controller { And I get the following errors: Code: A PHP Error was encountered I'm pretty tired and might have missed something small .. nice work so far though.
[eluser]GreatSlovakia[/eluser]
Hmm... guess I wasn't clear enough, so the ajax_config should point to a controller[slash]method which contains ONLY Code: $this->load->ajax_proxy(); Code: $this->load->ajax_model("Modelname"); Secondly it's a (very) good idea to place the Code: $this->load->ajax_model("Modelname"); So what you need to prepare before using it is the following (although you can customize it in lots of different ways ) - Core -- MY_Loader.php - Config -- config.php containing Code: $config['ajax_proxy'] = "ajax"; -- Ajax.php containing Code: <?php And lastly I am going to update the the customization portion of the OP with a cool way to use the __JS function in combination with security and user authentication in half an hour or so (currently working on a project where I am using it ) |
Welcome Guest, Not a member yet? Register Sign In |