[help] - Dynamic Text via Javascript

Get help using Construct 2

Post » Mon Jun 09, 2014 10:18 pm

Hey Everyone,

I am working with our developer who will be helping me do some back end development to load in dynamic content. He will be providing me some JavaScript that will load (3) dynamic variables. However, I have never messed with dynamic JS in construct and I am not sure how to post that data.

QUESTION:

1. How would I inject the (3) JS objects "artist,bio,trait" into Construct as text.
2. Do I need to make a custom plugin for this?


Quick "fake" JS snippet

Code: Select all
//this function goes into construct
function MusicToConstruct(data) {

  //here's a fake data object. Anytime the MusicToConstruct function is called by my code, it will contain this data object. So remove this when we get the real one working
  var data = {
    artist: 'Alestorm',
    bio: 'blah blah blah i\'m a long string',
    trait: 'pirate metal'
  }
  //end fake data object

}// JavaScript Document
B
13
S
1
Posts: 59
Reputation: 1,901

Post » Mon Jun 09, 2014 11:20 pm

On some_event set some_var
Code: Select all
Browser.ExecJS("window['obj_name']['prop']")

Where you've defined
Code: Select all
obj_name = {'prop':'Some text'}
globally;

Then if you need to fetch some_var from your script;
you can look for them in c2canvas.c2runtime.all_global_vars
Code: Select all
[{'name':'VariableName','data':'Value' .... }, { ... }];
B
10
S
2
G
1
Posts: 202
Reputation: 1,545

Post » Mon Jun 09, 2014 11:42 pm

Thanks Noncentz705 really appreciate the help.

Please forgive me if I may not be comprehending all this properly. "I am not an advanced developer"
I am still a bit confused how I set this up.

* this is the code I will add to my index.html
Code: Select all
function myFunction(data) {
  var data = {
    artist_name: 'Alestorm'
}


In terms of construct, still confused how I [set text] "artist_name" in the example above.
cs.jpg
You do not have the required permissions to view the files attached to this post.
B
13
S
1
Posts: 59
Reputation: 1,901

Post » Tue Jun 10, 2014 4:24 am

You're missing a '}'
Code: Select all
myFunction(){
  return{ 'prop' : 'some_value'};
}

Code: Select all
Browser.ExecJS("window['myFunction']()['prop']")


One other thing, don't forget to change your variable to 'text'
B
10
S
2
G
1
Posts: 202
Reputation: 1,545

Post » Tue Jun 10, 2014 5:44 am

Oh nice catch! Thank you very much for the help. Tested and it works :)

For others that need this in the future.

CAPX -
DynamicJavaScriptText.capx


JavaScript added to index.html

Code: Select all
     <script>
   function myFunction(){
  return{ 'prop' : 'some_value_works'};
}
</script>


This can also be simplified.

Code: Select all
       <script>
artist_name = "VALUE_2"
</script>


Code: Select all
Browser.ExecJS("artist_name")
You do not have the required permissions to view the files attached to this post.
B
13
S
1
Posts: 59
Reputation: 1,901

Post » Tue Jun 10, 2014 6:41 pm

-- REMOVED --
Last edited by Noncentz705 on Sun Jan 04, 2015 7:03 pm, edited 1 time in total.
B
10
S
2
G
1
Posts: 202
Reputation: 1,545

Post » Mon Aug 25, 2014 5:03 am

my callback is not working , gives an error in CocoonJS 'undefined is not an object (evaluating 'runtime.types.Function.instances')

Here is capx , https://dl.dropboxusercontent.com/u/800 ... lback.capx and modified index.html

https://dl.dropboxusercontent.com/u/80050021/index.html

Please help , I am also trying this method, solved-how-do-i-callback-in-outside-of-canvas_t109372 gives a different error ,

' undefined is not a object (evaluating 'IN['cr'].plugins_')
B
16
S
4
Posts: 248
Reputation: 2,267


Return to How do I....?

Who is online

Users browsing this forum: ksiads and 2 guests