2014년 12월 9일 화요일

[backbonejs] model undefined

I have a model undefined problem-it seems rather complex.I am trying to break the problem apart.


So , I have a clear question to make...during a POST request(and the accompanying response from the server)...is fetch() called at all?
I just want to test some assumptions.



First of all, it is other way around: POST (and others HTTP verbs) are performed by calling fetch (and other collection and model methods).
Secondly, fetch by default performs GET (because it fetches data, not sending it). 



Yes fetch() performs GET....
The weird thing though is that fetch is not called when POST is made.

There are two fetch commands in backbone....first of all to which you are referring to-in relation to POST requests.
I have placed breakpoints to both...but none seems to get called.



Look, if you make random POST (or GET or whatever) using some library (jQuery, whatever), Backbone could not magically catch it and call fetch (well, with jQuery you can actually can do something like this, but it will require a lot of global variables and will be very brittle).
And until you redefine Backbone.sync (or Collection#fetch, or Model#fetch), under no circumstances fetch will not perform POST requests.



Backbone(as you already know) requires jquery...POST requests are performed in essence by jquery/
Unfortunately I am to "zero" again....



Sure, it does. This also means Backbone knows about jQuery and jQuery doesn't have a clue about Backbone.   



I want to make one last questio...I am trying to use fetch() in the model....like this:

Event = Backbone.Model.extend({
        fetch:function(options)
        
        {
            console.log(options);
        }
     });

Is the above suppose to work?
because it does not,,,neither in POST or GET requests.

I am trying to override somehow fetch() 



It will kinda work, but it will never make request. 
When you override something, you usually should call parent class method. In Backbone you do it like this:
Event = Backbone.Model.extend({
        fetch:function(options)
        {
            console.log(options);
            Event.__super__.fetch.apply(this, arguments);
        }
     });



댓글 없음:

댓글 쓰기