Methods for structuring JavaScript SDKs

I’ve built a REST API and have been using Backbone models throughout a couple different applications to communicate to it.

I would really like to build a single JS SDK that can be used in any application that contains that reusable code.

I have started by creating an npm, bower, and component.json so that it can be easily installed.

From what I can gather the goal is to create a single object attached to the window for access within other applications. At which point everything in is encapsulated.

I have a couple questions and concerns:

  1. Is there anything I need to avoid to prevent conflicts with other libraries?
  2. How do I handle dependencies? Should I encapsulate them or let the user know they have to add it?
  3. Are there any methods of structuring SDKs that provide similar benefits to, say, MVC within a web app?

NOTE: If this is question doesn’t follow the guidelines for programmers.stackexchange.com please provide guidance on how to change it rather than just closing it. Thanks much.

3

  1. Is there anything I need to avoid to prevent conflicts with other libraries?

Simple answer is maybe, depending on your situation.

Example

jQuery by default adds itself as jQuery on the window object as well as $. While it is unlikely that another library is going to call itself jQuery (more on that later), the $ global is actually quite a common shortcut.

What jQuery does is save the variables $ and jQuery internally and on calling $.noConflict() will restore the previous set variables. [Reference]

Here is one very simple way of programming this:

var jQuery = function(){ return 1; };

(function()
{
    var _jQuery = jQuery;


    jQuery = function()
    {
        return 2;
    }

    jQuery.noConflict = function()
    {
        jQuery = _jQuery;
    }
})();

//Prints "2"
console.log(jQuery());

var jQuery2 = jQuery;
jQuery2.noConflict();

//Prints "1"
console.log(jQuery());

//Prints "2"
console.log(jQuery2());

My questions would be:

  • Are you wanting to use a common global variable (eg. $)?
  • What types of scenarios do you see with people working with your library?

If your SDK is going to use a common global variable and or you think people could be slinging a dozen different libraries while using your code, you may want to consider it. I will note that if you design your object/functions well enough, it shouldn’t be hard to add such functionality as I showed above later in your project.

  1. How do I handle dependencies? Should I encapsulate them or let the user know they have to add it?

How many dependencies are you thinking they might need? Are the dependencies quite common (ie. jQuery)?

Personally, if there are one or two dependencies, I would just state it in docs for your SDK. There is no need for you to go out and keep a copy of your dependencies internally maintained and tested.

Most jQuery plugins require jQuery (obviously) but don’t bundle it simply because they don’t need to. Generally these plugins state a minimum version of jQuery they support/have tested but not much beyond that.

While saying this, bundling can be useful especially if the API of your dependency isn’t very stable. There is one library I have used called Kalendae which bundles Moment.js. In this case, I have found it being bundled quite useful.

There are downsides to bundling linking back to your first point, they can cause conflicts if the person is already including a particular version of XYZ library. If you do bundle, do it in a way to cause the least amount of annoyance for future developers.

Without knowing what your specific dependencies are, I can’t really help you with whether under your case should you bundle or not.

  1. Are there any methods of structuring SDKs that provide similar benefits to, say, MVC within a web app?

I’m not exactly what you are asking here in relating SDK structure to MVC within a web app.

Depending what your SDK does and how people would likely integrate with it would explain how best to structure it.

In terms of general things to do:

  • Keep the global variables down to a minimum. Possibly wrap your code in a closure to avoid doing this (depending how your lay out your code).
  • Allow flexibility by allowing callbacks and events. Given you are doing a REST API, you might want to look into JS Promises. There are many libraries that provide such functionality and hopefully soon, browsers will provide this natively. [More info about futures/promises/delay in Computer Science]
  • Allow easy configuration/options to be passed to your functions. While it can be useful to add 20 different functions onto your object that configure a bunch of settings, it is far easier to allow the user to dump a JS object into a constructor or other function to do it all at once.

If you can explain more about that 3rd point, I might be able to provide a better answer.

Note

I mentioned that another library wouldn’t call itself jQuery in the global space. While that would generally hold true, if the person is using two different versions of jQuery on the same page (believe me, it happens) then it would run into issues. By default, calling $.noConflict() will only fix the $ reference in the global scope however $.noConflict has an optional parameter for also doing the same thing to the jQuery object.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật