bluedaniel
bluedaniel

Reputation: 2109

How to link Javascript file to the model folder in Zend

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.

Upvotes: 1

Views: 224

Answers (1)

Gordon
Gordon

Reputation: 316939

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.

Upvotes: 2

Related Questions