JQueryロード関数を使用してページの一部をロードしています。ロードするページのそのページから変数にアクセスできますか?例えば
ページAはJQueryロード関数を使用してBをロードします
ページBは、djangoオブジェクトを保持するpageB_varというコンテキストで変数をロードして設定します
ページAは、コンテキストに追加されたため、{{pageB_var}}を実行してこの変数にアクセスできます。
そうでない場合、これを行うための最良の方法は何ですか?
ありがとう
JQueryロード関数を使用してページの一部をロードしています。ロードするページのそのページから変数にアクセスできますか?例えば
ページAはJQueryロード関数を使用してBをロードします
ページBは、djangoオブジェクトを保持するpageB_varというコンテキストで変数をロードして設定します
ページAは、コンテキストに追加されたため、{{pageB_var}}を実行してこの変数にアクセスできます。
そうでない場合、これを行うための最良の方法は何ですか?
ありがとう
No. Page B's rendering context is irrelevant and unreachable by the time you get B's response.
Here's what happens: Page A is rendered in the server. during this time, its context exists. when the server is done rendering it, it sends the rendered page to the client. the client web browser then runs the javascript including your jquery load() to call the server again and tell it to render B. at this point the process that rendered page A doesn't exist anymore, so for page B to send stuff to page A's rendering you would have to make time travel....
The way to do this, is for page B to return a JSON object, and then use the (javascript) callback function given to load() to render changes to the page based on this JSON response from B.
It sounds like you're using an asynchronous request to update part of a page, receiving a partial HTML response and replacing part of the markup of your page with this response.
You've found that this obviously limits you to updating information contained within that partial page.
Consider instead providing a JSON response to your asynchronous request, containing all the information you need, and then updating the necessary HTML in any part of the page by manipulating the DOM client-side, with JavaScript. Think of the JSON as being your context for this purpose.
ページAがブラウザに送信されると、ほとんどの場合メモリに固定されます。表示内容を変更するには、JavaScriptのDOM関数を使用する必要があります。そうは言っても、ページBへの呼び出しのビューからJSONを返し、それをデコードしてページAに挿入することができます。