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();
}
});
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'
if(data.message == "reload"){
oTable.ajax.reload();
}
});
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('363be561b4f2ca6eed 4c', {
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()->dealershi p_id == ""){?>
dealer = "all";
<?php } else { ?>
dealer = <?php echo Auth::user()->dealership_i d;?>;
<?php } ?>
var channel = pusher.subscribe(dealer);
channel.bind('call_change' , function(data) {
if(data.message == "reload"){
oTable.ajax.reload();
}
});
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('363be561b4f2ca6eed
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()->dealershi
dealer = "all";
<?php } else { ?>
dealer = <?php echo Auth::user()->dealership_i
<?php } ?>
var channel = pusher.subscribe(dealer);
channel.bind('call_change'
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.
I am guessing the problem is somewhere else?
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>
Working sample hereI am guessing the problem is somewhere else?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry to take so long. This was a better way of writing it by far.
Thanks very much
Thanks very much
You are welcome.
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