weixin_33713350 2013-06-18 15:55 采纳率: 0%
浏览 23

剃刀比阿贾克斯还锋利吗? [关闭]

                <div class="grid--cell fl1 lh-lg">
                    <div class="grid--cell fl1 lh-lg">
                        As it currently stands, this question is not a good fit for our Q&amp;A format. We expect answers to be supported by facts, references,   or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question   can be improved and possibly reopened, <a href="/help/reopen-questions">visit the help center</a> for guidance.

                    </div>
                </div>
            </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2013-06-18 17:59:47Z" class="relativetime">6 years ago</span>.</div>
        </div>
    </aside>

I know jQuery is currently everybody's darling hammer (truth be told, I'm enamored with it, too), but I'm wondering if dynamically generating html might be more elegantly, or more easily, done using Razor/C#. For example, to update a page, consider this Razor pseudocode:

@{
   var divContent = functions.getDivContent("Platypus");

   if(IsPost)
   {
      divContent = functions.getDivContent(request[mammalName]);
   }
   else
   {
      ;//anything?
   }
}

//The getDivContent() function in functions (not shown) returns dynamically generated html

HTML pseudocode:

<body>
<form method="POST" action="" >
  <input type="Select" name="mammalName" ... />
</form>
  <div>@divContent</div>
</body>

jQuery pseudocode:

$('mammalName').selectionChanged() {
    $('form').submit();
});

Doing all of this in a jQuery function, such as using json data and calling .getJson(), would obviate the need for a complete page refresh, but in either case the server has to be queried for data, so I don't know if that's really such an advantage, especially in the case (mine) where the part of the page being refreshed is the lion's share of the page.

Is there a flaw in my thinking/design? Is there a compelling reason to choose jQuery over Razor here?

</div>
  • 写回答

1条回答 默认 最新

  • ?yb? 2013-06-18 16:12
    关注

    Yes, there are definitely things to consider when choosing to change pages using ajax or to change pages using a whole page reload.

    performance-wise, using ajax will always come out on top because you're only requesting the entire page once, and then using ajax to request partials.

    As far as maintenance, you don't actually need to change anything server-side to use ajax rather than Razor. The only added maintenance will be coding your client-side code to properly handle the fact that the page isn't going to reload.

    If you used Ajax, you could optimize your requests FOR ajax, making your pages return only the needed content rather than the entire page, further improving performance. This comes at a slight cost to maintenance because it's adds another layer of complexity, however, if done properly, is easy to maintain.

    UX wise, ajax will generally be better due to not having to reload the page. There is a loss of back-button functionality unless you also code that into your client-side ( also not very difficult ).

    If you add SEO into the equation, things get a bit more difficult unless you coded your site FIRST to work without ajax, then added ajax functionality on top of it that way the crawlers can still access the entire site without ajax.

    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办