locked
Windows Apps | WinJS.Binding.as returns function in a string mode

    Question

  • I got this Script: 

    (function () {
    
        var app = WinJS.Application;
    
        
        WinJS.Namespace.define("Example", {
    
            randsFunction: WinJS.Binding.as ( {
    
                randNum:function(){
    
                    var num = 0; setInterval(
    
                        function () {
                            num = Math.floor((Math.random() * 100) + 1);
                        }, 1000)
    
                    return num;
                }
            })
        });
    
        app.onactivated = function () {
    
            Example.randsFunction.bind('randNum', function (value) {
    
           var d = document.getElementById('myDiv');
    
           d.textContent = value;
           
        })
    }
    
        app.start();
    
    }());

    When ran, the content inside the div prints:function () {
    var num = 0; setInterval( function () { num = Math.floor((Math.random() * 100) + 1); }, 1000)
    return num; }

    It seems like WinJS.Binding cannot differentiate between a plain string and/or a function. Or what do you think is the problem? Any suggestion is highly appreciated

    Thursday, May 1, 2014 11:40 AM

Answers

  • I think the problem is that you're expecting it to return a function, when the documentation says it's supposed to return an object:

    WinJS.Binding.as function

    Returns an observable object. This may be an observable proxy for the specified object, an existing proxy, or the specified object itself if it directly supports observation.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, May 2, 2014 2:20 PM
    Moderator

All replies

  • I think the problem is that you're expecting it to return a function, when the documentation says it's supposed to return an object:

    WinJS.Binding.as function

    Returns an observable object. This may be an observable proxy for the specified object, an existing proxy, or the specified object itself if it directly supports observation.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, May 2, 2014 2:20 PM
    Moderator
  • Thanks alot... but how do i do an

    eval()


    on that function to make it part of the <script> as a real 

    function

    and not a mere string??.... Any Idea?... In other words, how do I add a function like:

    var myString = "function shout(){ alert('HAHAHAHA'); }"; 

    How to add that function in the project so as TO run the alert?.... using WinJS?


    • Edited by ErickBest1 Monday, May 12, 2014 6:09 AM
    Monday, May 12, 2014 6:03 AM