weixin_33749242 2013-11-05 15:50 采纳率: 0%
浏览 48

jQuery在ajax中加载的内容

My application is supposed to be a single page application and I have the following code that works fine:

home.php:

<div id="container">
</div>

accordion.php:

    //Click functions: load content 
    $('#parents').click(function(){
        //Load parent in container
        $('#container').load('http://www.blabla.com/entities/parents/parents.php');
    }); 

parents.php:

<div class="entity_wrapper">
    Some divs and selectors 
</div>
<script type="text/javascript">
    $(document).ready(function(){
        //Some jQuery / javascript 
    }); 
</script>

So the content loads fine, while the scripts dynamically loaded execute fine as well.
I apply this system repetitively and it continues to work smoothly.
I've seen that there are a lot of frameworks available on SPA's (such as backbone.js) but I don't understand why I need them if this works fine.

From the backbone.js website:

When working on a web application that involves a lot of JavaScript, one of the first things you learn is to stop tying your data to the DOM. It's all too easy to create JavaScript applications that end up as tangled piles of jQuery selectors and callbacks, all trying frantically to keep data in sync between the HTML UI, your JavaScript logic, and the database on your server. For rich client-side applications, a more structured approach is often helpful.

Well, I totally don't have the feeling that I'm going through the stuff they mention.
Adding the javascript per page works really well for me.
They are html containers with clear scope and the javascript is just related to that part.
More over, for me the front end doesn't do that much, most of the logic is managed based on Ajax calls to external PHP scripts. Sometimes the js can be a bit more extended for some functionalities, but all just loads as smooth in less than a second.

If you think that this is bad coding, please tell me why I cannot do this and more importantly, what is the alternative I should apply. At the moment, I really don't see a reason on why I would change this approach as it just works too well.

I'm kinda stuck on this question because it just worries me sick as it seems too easy to be true. Why would people go through hard times if it would be as easy as this...

Edited question based on Juhana's comment:

I don't feel like my project fits the use case Backbone is made for. Should I use it anyway?

  • 写回答

1条回答 默认 最新

  • derek5. 2013-11-05 16:01
    关注

    It all depends on the level of your apps scalability, ease of maintenance and performance. Looking at your code, you use some old (and to be deprecated) jQuery features so i gather that it was written quite some time ago.

    Know that a single page application doesn't ends with data loading and also contains:

    • Event bindings
    • dynamic data bindings
    • multiple views
    • reusable UI
    • flexible and non-dependant UI controls
    • JS logic for parsing, displaying, keeping track and caching your data

    For the past year i've been working on an analytics platform that doesn't use a single framework and by chance - we had to implement everything on our own (and done so quite successfully). It's scalable, robust, easy to understand and quite good on the performance bit, however using angular, ember or knockout would have saved us plenty of time since most maintenance is about the level of abstraction of our various core features (detching data, parsing it, keeping track of it etc).

    It's also great for testing, which is a huge benefit when your system gets big, since the most important aspect of testing, in my opinion alone - is the dependency tracking it allows so basically, you can stop worrying about working on something - and ruining things that are seemingly unrelated

    I think that although client side frameworks have a certain learning curve, some faster than others - it's worth your time since it takes away some of the routines one must constantly implement in his or hers development process.

    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能