Need Example .injection Code

Discuss your code, get questions answered

Need Example .injection Code

Postby ebkwall » Sun Apr 10, 2016 8:49 am

I have been working off the documentation on this site for Custom Datatypes to create a simple Geography type. To start with the instantiable class will simply be a latitude and longitude and possibly a method to return a map image.

The static template class will override "evaluate" or better yet "draw" to either call a method on the base object or directly reach out to a web source and plot the lat lon on a map and display it. I was working off the NDImage sample and the Color type I found in the shared folders. I think I am close but I cannot view the Definition of the Color ".injection" method which I think is the key that I need.

Is that posted anywhere in the documentation or in this forum anywhere? Or is there a process to view it in the Data folder that I don't know about? Does anyone have a complete .injection method sample for a Custom Datatype?

Thanks much,
Brian
ebkwall
 
Posts: 22
Joined: Tue Apr 05, 2016 2:29 pm

Re: Need Example .injection Code

Postby oliver » Sun Apr 10, 2016 12:33 pm

Wow, you're going fast.

I trust you saw the Extensions section http://forums.naivedesign.com/viewforum.php?f=11 of the forum, where there's some discussion about and code for extensions.

You can look at existing injection code in one of two ways:
- in the calculator itself: while hidden from display on softkeys, the .injection variable is still accessible: while in a folder that has one (Chem, Color, ...) type .injection and press Enter. Then press the Edit key.
- in the calculator definition: tap the Definition tab. Scroll to the bottom, and tap on Injection. This is the actual complete injection code that gets injected every time you switch to the calculator tab. This is where stuff goes when you press Install. You can edit the code here and experiment. (Unfortunately, large amounts of JavaScript code and the small phone screen don't mix very well. To write a data type from scratch, it would be better to use a computer and transfer the data, as mentioned elsewhere.)

Glad you're looking into this! I think I say this in various posts, but custom data types are probably the best thing about the app...

For your specific application, you may want to look into creating a <div>, inline-CSS styling it for the dimensions you want, and instantiating a google.maps.Map object with it. The Google Maps API allows you to freely position and draw markers and a lot more.
oliver
Site Admin
 
Posts: 433
Joined: Sat May 01, 2010 2:11 pm

Re: Need Example .injection Code

Postby ebkwall » Sun Apr 10, 2016 2:05 pm

Great! lots of good info in those links.

I am missing some critical (though most certainly simple) step.

I have an extremely basic type, just so I can actually create one of my own.
When I create the .injection entry in my Geography folder it does not look the same as it does in the Color folder.

I will email you some screen shots as posting them here is kind of difficult for me at the moment.
To describe what I am seeing in the Color vs Geography folders .injection entries:
Geography.injection entry I see:
.injection
/* Geography type definition */ var Geography = {...

in other words, I see the code I entered for the Definition

in the Color however I do not see the beginning of the Color definition code. Instead I see:
.injection
{"type":"code","stringVal... (6.58 KiB)

leading me to believe I created my Geography.injection entry incorrectly.

Here is what I am pasting in for the .injection definition.
I am pretty sure it has all the required properties though it started out much more ambitious than this.

Code: Select all
/* Geography type definition */
var Geography = {
    type: "geography",
    isLoaded: true,
    toString: function(obj) { return obj.lat + ", "+ obj.lon; },

    instance: function(location) {
      //parse location string for lat and lon - just some dummy data for now
      this.lat = 36;
      this.lon = -160;

      this.type = Geography.type;
    }
};
calculator.registerType(Geography);



Every time I run the install code

Code: Select all
<< "geography" .injection inject >>


I get an
Program execution error
Detail: wrong type of argument
dialog.


Thanks,
Brian
ebkwall
 
Posts: 22
Joined: Tue Apr 05, 2016 2:29 pm

Re: Need Example .injection Code

Postby ebkwall » Sun Apr 10, 2016 2:16 pm

I seem to have stumbled across the answer.

I pasted the code into the RPL line, Enter, '.injection, STO
Bingo. The Install executes and my datatype is in the calculator.

Now to flesh it out and learn how to work with it.

Thanks!
Brian
ebkwall
 
Posts: 22
Joined: Tue Apr 05, 2016 2:29 pm

Re: Need Example .injection Code

Postby oliver » Sun Apr 10, 2016 5:01 pm

Awesome!

I just posted the code for Code here: http://forums.naivedesign.com/viewtopic.php?f=11&t=1027

There you can see the code for the inject function. It's only defined on Code objects. If you try to run a known function on the wrong kind of object (I think you had an expression before) you get the error message you got. You can always run the command typeof to see what kind of object you have.

Any input that starts with /* will be automatically turned into a Code object. That's why it worked when you pushed your code using the input line.
(Note, JavaScript code that doesn't start with a comment would not be recognized as Code. Clearly, there could be a different/better detection of JavaScript code.)

Your code looks fine. Have fun adding functions to it! A geo type sounds like a cool thing to have!
oliver
Site Admin
 
Posts: 433
Joined: Sat May 01, 2010 2:11 pm

Re: Need Example .injection Code

Postby ebkwall » Sun Apr 10, 2016 6:11 pm

I have been making (very slow) progress. It seems my comments were causing issues.

It was not until I stripped out all but bare bones - including comments - that I saw any evidence my Geo type was working. I think I saw somewhere either in the documentation or on this forum that there may be some issues depending on the editor being used? I am using NotePad++

Anyway I have it creating an object via the fromString given a recognized string is entered.
toString, toHTML, and eval all work. Trying to create an instance given a lat and lon entered on the stack as decimal numbers. I think I've seen an example of that here somewhere. What is the syntax?

36.432 enter
-173.123 enter
"what goes here" enter

This will be fun to work on this week.
Thanks!
ebkwall
 
Posts: 22
Joined: Tue Apr 05, 2016 2:29 pm

Re: Need Example .injection Code

Postby oliver » Mon Apr 11, 2016 10:48 pm

Ah, yes, ok.

Your object wants/needs two more things:
- a "factory" function that uses the new operator on the instance() method to construct a new instance of your type
- an onload() function that blends a new function into the top-level calculator function collection to make the factory function accessible

Let's look at Color for an example.

Factory function:
Code: Select all
   fromNumber: function(x) {
      return new Color.instance([x&0xff, x&0xff00 >> 8, x&ff0000 >> 16]);
   },

We take a number and use it to create an instance. (For clarity, there's nothing special about "instance". The framework doesn't know about it. We need ourselves to use it.

Real-valued top-level function:
Code: Select all
   onload: function() {
      ...
      // add real "constructor"
      calculator.functions["toColor"] = Color.fromNumber;
      ...
   }

Here we create a top-level reference "toColor" that points to our factory function. We put it into the function collection that corresponds to our input datatype. Top-level means "real-valued". calculator.functions.string would be "string-valued", calculator.function.vector would be "array-valued", etc.

Now the only magic is the framework will determine the arity of our factory function and pull as many values from the stack as are needed to satisfy it. That part happens automatic without that we have to do anything.

30 toColor then will invoke our factory function and create a new instance that uses the value 30.

So, for your geo datatype, I suggest you do something like this:
Factory function:
Code: Select all
   fromLatLong: function(lat, long) {
      return new Geography.instance(lat, long);
   },


Top-level function:
Code: Select all
   onload: function() {
      // add real "constructor"
      calculator.functions["toGeo"] = Geography.fromLatLong;
      // optionally, define function name alias (just for fun/shorter name)
      calculator.function_aliases["geo"] = "toGeo";
   }


Then, you should be able to instantiate your type like this: 23.2 56.6 toGeo
oliver
Site Admin
 
Posts: 433
Joined: Sat May 01, 2010 2:11 pm


Return to Programming

Who is online

Users browsing this forum: Google [Bot] and 1 guest

cron