Link to home
Start Free TrialLog in
Avatar of tjyoung
tjyoung

asked on

Trouble inserting a variable in javascript

I'm stuck. Perhaps I'm missing something very obvious, so please don't overlook that possibility :)

Simplified version: I'm creating a variable, and looking to insert that variable into below. Its not inserting. Just showing the word 'dealer' in the brackets for 'subscribe instead of the word "all".

var dealer;
dealer = "all";

var channel = pusher.subscribe(dealer);
    channel.bind('call_change', function(data) {
      if(data.message == "reload"){
            oTable.ajax.reload();
      }
      });
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

What is the context for this request? You appear to be making use of a library / plugin but you have not told us what it is.

Tell us what you are expecting the code to do
What it actually does
Any other information regarding external resources the code is dependent on
Avatar of tjyoung
tjyoung

ASKER

HI Julian.
I'm using pusher.com to refresh a table in real time when a phone call comes in.
Works great elsewhere, except this time the user who is logged in looking at the table, may or may not have a 'dealership_id' assigned to their user profile.

So I need to insert their dealership_id which would create a channel that pusher uses to push events to ( in this case, its if a call comes in to the dealership, refresh the table to show the new call info)



var pusher = new Pusher('363be561b4f2ca6eed4c', {
    encrypted: true,
    disabledTransports: ["sockjs"]
    });

 // this is what I'm using to determine if the dealer variable should be 'all' or a specific dealership_id based on their profile. I know its a mishmash of php at the moment, just putting it together quick to ensure all works. What I don't understand is: I'm creating a javascript variable 'dealer' and using php I'm assigning it a value.

Then I'm trying to write that value where it says 'pusher.subscribe()' but its not writing in the value. Its just writing the word 'dealer' not the dealer value
 
var dealer;

<?php
if(Auth::user()->dealership_id == ""){?>
      dealer = "all";
<?php } else { ?>
      dealer = <?php echo Auth::user()->dealership_id;?>;
<?php } ?>

 var channel = pusher.subscribe(dealer);
    channel.bind('call_change', function(data) {
      if(data.message == "reload"){
            oTable.ajax.reload();
      }
      });
Have you resolved this question?

This code seems to work as expected. It uses your code above inside a mockup of the pusher env.

<script>
var dealer;

<?php
class Auth
{
  static function user()
  {
    $return  = new stdClass;
    $return->dealership_id = '';
    
    return $return;
  }
}

if(Auth::user()->dealership_id == ""){?>
      dealer = "all";
<?php } else { ?>
      dealer = <?php echo Auth::user()->dealership_id;?>;
<?php } 
?>

var pusher = {
  subscribe: function(dealer) {
    console.log('Dealer: [' + dealer + '] subscribed');
    return {
      bind : function(type, callback) {
        console.log('Bind called with type: ' + type);
        data = {
          message: 'reload'
        }
      }
    }
  }
}

var oTable = {
  ajax: {
    reload: function() {
      console.log('reload called');
    }
  }
}
 var channel = pusher.subscribe(dealer);
    channel.bind('call_change', function(data) {
      if(data.message == "reload"){
            oTable.ajax.reload();
      }
      }); 
</script>

Open in new window

Working sample here

I am guessing the problem is somewhere else?
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tjyoung

ASKER

Sorry to take so long. This was a better way of writing it by far.
Thanks very much
You are welcome.