Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: Array as return type

$
0
0
Yeah, option 1 is exactly what Ive done. (Or so I think.)

This is what I do:
    public class ElementCollection : List<Element>
    {
        [ScriptMember(Name = "text")]
        public string[] Text()
        {
            // Desired output.
            var s = this.Where(e => e.Text() != "").Select(e => e.Text()).ToArray();

            // Test to compare types
            var a = new[] { "Adolph", "Blaine", "Charles", "David" };

            return s;
        }
   }
In javascript I then populate a part of an object with this.

I actually first encountered this problem when I tried to JSON.stringify the populated object. (Circular reference error)

Anyway, in JS I do:
function parseArticles(page) {
    var c = page.element.findAll("div.article_content").text();

    var a = { "header": h, "content": c };

    // Type test
    var myArray = ["1", "2", "3"];

    // myArray.getName() returns Array
    $eng.debug("TYPENAME Array:" + myArray.getName()); 
    
    // c.getName() returns HostObject
    $eng.debug("TYPENAME:"+c.getName());

    $eng.out(a);
}
$eng.out() then tries to make JSON out of it and it crashes. But thats beside the point, myArray and a (in the second example) has different types, which is my problem. (I think)

I never explicitly expose the ElementCollection class to the engine, its just returned from a different host object.

The getName() function is ripped from Stack Overflow, its this:
Object.prototype.getName = function () {
    var funcNameRegex = /function (.{1,})\(/;
    var results = (funcNameRegex).exec((this).constructor.toString());
    return (results && results.length > 1) ? results[1] : "";
};

Viewing all articles
Browse latest Browse all 2297

Trending Articles