Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c.apply is not a function

This code give me this error: c.apply is not a function All code works well only if i use one function. However i am not sure about how use two functions. These lines are probably wrong :

 postHandler(<?php echo get_posts($db, 0, $_SESSION['posts_start']); ?>, <?php echo get_posts1($db, 0, $_SESSION['posts_start']); ?>);

and

var postHandler = function(postsJSON, postsJSON1) {
$.each(postsJSON, postsJSON1, function(i, post, post1) {

script

first function

function get_posts($db, $start, $number_of_posts) {
       //code
            return json_encode($posts);
        }

output:

string '[{"username":"Altitude software","foto_oferta":"thumb\/miniaturas\/oferta\/default_offer.jpg","actividades":"Some activities","id_oferta":77,"oferta":"Programador web" ...

second function

function get_posts1($db, $start, $number_of_posts) {
       //code
            return json_encode($posts1);
        }

output:

string '[{"id_offer":77,"tags":["c++","JAVA"]},{"id_offer":76,"tags":["ajax","php"]},{"id_offer":75,"tags":["PHP","JAVA"]}]'

js

var postHandler = function(postsJSON, postsJSON1) {
                $.each(postsJSON, postsJSON1, function(i, post, post1) {
                    var id = 'post-' + post.id_oferta;

                    $('<div></div>').addClass('post').attr('id',id)
                    .html('<div class="box_offers"><div class="rating_offer"></div><div class="post-title">' 
                            + post.oferta + '</div>  <div class="post-box"> <a class="oferta_val bold_username">'
                            + post1.tags + '</a></div><a id='+id+'hide class="post-more" >Descrição</a><div class="logo_offer">')
                            .appendTo($('#posts'));

                    $('#'+id+'hide').click(function() {
                        $('.'+id+'hidden').slideToggle('fast');
                    });
                }); 
            };

postHandler(<?php echo get_posts($db, 0, $_SESSION['posts_start']); ?>, <?php echo get_posts1($db, 0, $_SESSION['posts_start']); ?>);
like image 728
user455318 Avatar asked Jan 09 '12 01:01

user455318


1 Answers

I believe the problem is this line:

$.each(postsJSON, postsJSON1, function(i, post, post1) { 

The generic iterator $.each() function only takes two parameters, the second of which is supposed to be a function. Similarly the callback function you provide is supposed to take two parameters.

What is your intention as far as supplying two objects to iterate over at the same time? If you can describe your data structures and explain what you want to do I could make some suggestions. (Show your JSON...)

UPDATE: OK, based on the question update both postsJSON and postsJSON1 are arrays. Given the way that you were trying to use both from inside the same function it appears that the elements within the arrays have a one-to-one relationship, that is, postsJSON[0] relates to postsJSON1[0], postsJSON[1] relates to postsJSON1[1], postsJSON[2] relates to postsJSON1[2], and so on and so forth.

If that is the case the smallest possible change to your existing code to get it to work would be this:

var postHandler = function(postsJSON, postsJSON1) {
   $.each(postsJSON, function(i, post) {
      var post1 = postsJSON1[i];

      // the rest of your code from inside your $.each() here
   });
};

That is, continue to use $.each(), but noting that it can only directly iterate over one array at a time use the provided index i to iterate over the other array in parallel by setting that up manually as the first line of the function.

Or perhaps the more obvious way to do it is with a traditional for loop:

var postHandler = function(postsJSON, postsJSON1) {
   var i, post, post1;
   for (i = 0; i < postsJSON.length; i++) {
      post = postsJSON[i];
      post1 = postsJSON1[i];

      // the rest of your code from inside your $.each() here
   }    
};

Either way will process both arrays in parallel. Obviously this assumes the arrays are the same length and that the items at any given index number will be the items that you want to relate to each other as mentioned above - if not then you will need to provide more detail about how the arrays are related.

like image 71
nnnnnn Avatar answered Sep 19 '22 13:09

nnnnnn