1

I am having trouble trying to get this to run. I am trying to use the observer pattern. I wnant to be able to run a arbitry amount of functions on the select change event. I keep getting an error 'Publisher.protoype' is null or not an object. What am I doing wrong?

        function Publisher(){
            this.subscribers = [];
        }

        Publisher.protoype.deliver = function(data){
            this.subscribers.forEach(
                function(fn){
                    fn(data);
                }
            );
            return this;
        }

        Function.prototype.subscribe = function(publisher){
            var that = this;
            var AlreadyExists = publisher.subscribers.some(
                function(el){
                    if (el == that){
                        return;
                    }
                }
            );
            if(!AlreadyExists){
                publisher.subscribers.push(this);
            }
            return this;
        }

        Function.prototype.unsubscribe = function(publisher){
            var that = this;
            publisher.subscribers = publisher.subscribers.filter(
                function(el){
                    if(el != that){
                        return el;
                    }
                }
            );
            return this;
        }
        var EventPublisher = new Publisher();      
        var SelectChange = function(data){alert("hello")};
        SelectChange.subscribe(EventPublisher);
        function onSelectChange(oSelect){
            EventPublisher.deliver(oSelect);
        }

    </script>

</head>

<body>
    <form name="Tester" action="Tester" method="post" enctype="application/x-www-form-urlencoded">
        <select name="selecter" onchange="Javascript:onSelectChange(this)">
            <option name="Shane" value="Shane">
                Shane
            </option>

            <option name="Shane2" value="Shane2">
                Shane2
            </option>
        </select><input type="submit"><input type="reset">
    </form>
</body>
</html>

2 Answers 2

2

You have a typo: Publisher.protoype.deliver is missing a 't'.

Sign up to request clarification or add additional context in comments.

2 Comments

Well tha deserves a face slap! :P Thanks for that!
@Shane - missing a t in that comment too. Might want to have your keyboard checked ;)
0

Publisher.protoype is definitely null or not an object. Perhaps you meant to type Publisher.prototype.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.