views:

28

answers:

1

Ok so Ive got this Javascript file, simply:

$(document).ready(function() {
    $('.status').prepend("<div class='score_this'>(<a href='#'>score this item</a>)</div>");
    $('.score_this').click(function(){
        $(this).slideUp();
        return false;
    });

    $('.score a').click(function() {
        $(this).parent().parent().parent().addClass('scored');
        $.get("/js/Rating.php" + $(this).attr("href") +"&update=true", {}, function(data){
            $('.scored').fadeOut("normal",function() {
                $(this).html(data);
                $(this).fadeIn();
                $(this).removeClass('scored');
            });
        });
        return false; 
    });
});

Where it says /js/Rating.php, that file is in application/modules/recipes/models/Rating.php, this is because it contains some Zend logic that cant be accessed outside the application.

+1  A: 

Through the Controller. That is the only reasonable way.

Remember JavaScript is executed on the client side, not the server. You placed your models outside the publicly accessible webroot, because your users are supposed to access your MVC app (e.g. the domain logic in the Rating model) through your app's FrontController and not on the Model directly.

In other words, since .get() issues an Ajax Request, you should direct this request to the Ratings controller in your app and make the Ratings controller call the appropriate model in your application. The model returns something to the controller and the controller then returns this result to the client.

Example:

$.get("/rating/update/someParamId/someParamValue");

This assumes you are using the default rewrite rules. In your controller you could then do $this->getRequest()->getParam('someParamId') and it would contain 'someParamValue'. The controller will return the ViewScript usually rendered by the action back to the client, so you might want to disable the layout to just return the HTML fragment.

Gordon
So If I had a ratings controller, how would I change the javascript to get redirect to this, the full url?
bluedaniel
Yep, you're doing it wrong at the minute. I would assume you have a Ratings Controller as Gordon mentions, you would probably call the action something like update, and then your URL should be /ratings/update.That should solve it, but I can't help with writing the actual code :)
Stephen Orr
i'm agree with this answer. For example, you can create a rating controller and get it like simple page throught JS, but parameters pass using POST method, it actually doesn't matter. Also, in ideal get response from the script JSON for checking errors and so on.
Ok I understand working through the mvc structure, hacking a script to work into this model can be very though. Also I think the level of ajax knowledge is a bit beyond me at this moment in the project.
bluedaniel