How to use backbone js with some legacy plugins? - plugins

I've working on a project and I'm using some jquery plugins, right now I'm trying to update my code to use backboje js but it's not clear how to put together those old plugins with backbone js.
the most important plugin I want to use is jcvl (http://code.google.com/p/jcvl/) but I'm trying to put this question general to get more ideas about how to integrate any pluggin with backbone.

Backbone only creates one global variable, Backbone, so there shouldn't be any conflicts with any jQuery plugins. Backbone also depends on Underscore.js, which also only creates one global variable, _, so it also shouldn't cause any conflicts. And if there is a conflict, both Backbone and Underscore.js offer you a noConflict() option.
I've been using Backbone with jQuery plugins for a while and haven't encountered any problems. You would use the plugin in the same way as before you introduced Backbone. For example:
var MyView = Backbone.View.extend({
render: function(){
$(this.el).html('<div class="foo"></div>');
this.$('.foo').somejQueryPlugin();
}
});

Related

Creating custom plugin for Converse.js 9.1.1

I downloaded conversejs 9.1.1 and I am trying to learn the plugin architecture by making my own custom plugin. I looked at the http-auth plugin here to see how to add a plugin.
https://github.com/conversejs/community-plugins/tree/master/packages/http-auth
To install the plugin it directs me to the instructions here:
https://m.conversejs.org/docs/html/plugin_development.html
I understand I have to modify my webpage to whitelist the plugin, but for some reason I can't grok a few things. Here is my awesome plugin which resides in a file called Hello-World.js
import { converse } from "#converse/headless/core";
const plugin = {
initialize() {
console.error("Hello World!")
}
}
if (typeof converse === "undefined") {
window.addEventListener(
'converse-loaded',
() => converse.plugins.add("Hello-World", plugin)
);
} else {
converse.plugins.add("Hello-World", plugin);
}
The htpp-auth.js has no imports, but WebStorm was complaining that converse was unknown so I had to add the import. Why does the http-auth plugin not have to do that?
I am not sure where the plugin code is supposed to live. I added Hello-World under src/plugins/Hello-World. Is this correct?
Maybe related to above, but to get the plugin to actually run in addition to whitelisting it in my webpage I had to modify converse.js and add import "./plugins/Hello-World/Hello-World.js" which makes me think I am missing something obvious as I would think adding a plugin shouldn't require you to change the base code.
If it matters I am testing my plugin by running make serve in the conversejs directory and directing my web-browser (Chrome) to localhost:8000/fullscreen.html
Thanks, Wray
The htpp-auth.js has no imports, but WebStorm was complaining that converse was unknown so I had to add the import. Why does the http-auth plugin not have to do that?
converse is available as a global once converse.js has been loaded via the <script> tag.
That's why there's the if (typeof converse === "undefined") { check at the end of the plugin. It waits for converse.js to be loaded if converse isn't yet defined.
I am not sure where the plugin code is supposed to live. I added Hello-World under src/plugins/Hello-World. Is this correct?
Most community plugins are developed in such a way that they're loaded separately via <script> tags. If you do it like that, it doesn't matter where they live.
Maybe related to above, but to get the plugin to actually run in addition to whitelisting it in my webpage I had to modify converse.js and add import "./plugins/Hello-World/Hello-World.js" which makes me think I am missing something obvious as I would think adding a plugin shouldn't require you to change the base code.
You can do it like that if you want to include your plugin inside a custom build of Converse, then you can also import stuff from converse.
The alternative is to load your plugin separately via a <script> tag, but then you can't import stuff and have to use the converse global and the closured _converse object that is passed to your plugin's initialize function.

Using Svelte JS framework with a Web Component library, how can I accomplish two-binding with the custom elements via the bind:value directive?

Note: There is a GitHub issue open on the Svelte repo for this very question.
https://github.com/sveltejs/svelte/issues/4838
While I understand that this may or may not someday become a feature of Svelte, what I am asking is how I can create a workaround today to support a third-party web component library like Shoelace or UI5, or Ionic with two-way binding?
Synopsis:
I have set up a Svelte app and successfully added a Web Component Library (Ex: Shoelace).
I write a line of code that uses two-way binding like:
<sl-input name="name" type="text" label="Name" bind:value={name}/>
I cannot two-way bind (bind:value={}) because the Svelte compiler doesn't recognize the valid bindings for the custom web components.
If there were a wrapper library that you know of, or some other thing I could do to make it work, that would be great.
I am not sure if this will work with libraries but it is worth a shot. For my custom component I just followed this answer.
Add this in your custom component:
<script>
export let onValueChange;
export let value;
$: onValueChange(value);
</script>
And add this when using the component (it will give an error until you add this)
<custom-component onValueChange="{(x) => value = x}"/>

$(...).datetimepicker is not a function when use converse.js

I want to implement XMPP chat functionality in my system using converse.js for client side chat interface. but when i use converse.js in my layout page, browser showing me error like
$(...).datetimepicker is not a function
$(...).dataTable is not a function.
I have used bootstrap datetimepicker and datatables. It seems like jquery conflictions.
I have tried to resolve conflictions by changing place of some jquery files. but i didn't get success. So how can i remove conflictions?
EDIT: As of version 3.0.1 this shouldn't be an issue anymore. In previous builds the $.noConflict call wasn't being made. This is now fixed in 3.0.1. If you're using an older version, then the text below is still relevant.
Converse.js comes bundled with jQuery. It uses jQuery's noConflict method to relinquish control of the $ variable and therefore to avoid conflicts with other versions of jQuery, but apparently this doesn't always work reliably.
There are a few things you can try:
Load converse.js before all your other JS libraries.
Alternatively, drop your own jQuery and instead use the one included in converse.js. You can access it via converse.env.jQuery.
Or alternatively use the converse.js bundle that doesn't include jQuery: https://cdn.conversejs.org/dist/converse-no-jquery.min.js

SystemJS - Provide JQuery global object

I have a project which is a mix of new code and old libraries, some of which is based on JQuery and JQuery plugins. I'm facing some issues where some plugins are using the global object ($) to integrate, and other are doing require("jquery") or the equivalent. This appears to be fetching two different instances of JQuery.
Because of some existing code, I need the global object to be there. However, to support my newer code I want anything that requires or imports 'jquery' to be provided the window.jQuery object instead of trying to load an external module.
Can I do this with SystemJS?
Did you try the SystemJS-Adapter-Modules? Create a file jquery.js and reference the globally loaded jQuery function:
module.exports = window.jQuery;
When you import "jquery" you should get a reference to the already loaded jQuery function.
SystemJS-Adapter-Modules

PlayFramework with Scala, WebJars, ReactJS and RequireJS?

I am looking for an example about combining the four technologies in the title :) I have a working ReactJS application using Play, Scala and WebJars, it's here on GitHub.
Now I would like to add RequireJS, but I'm not sure how to go, especially because it seems to require a different JSXTransformer? If anybody has a pointer (or even a PR) it would be very appreciated.
This is not the easiest thing to do, but I was able to pull it off. I will give you some tips here. I also uploaded my own project to github. This is a very simple snake game built using React and RequireJs. It based on Webjars and Play Framework.
Remember that RequireJs is part of Play Framework. Here's a step by step guide to create React/RequireJs/WebJars/Play integration:
In your plugins.sbt add addSbtPlugin("com.github.ddispaltro" % "sbt-reactjs" % "0.5.2"). This is a plugin which transforms JSXes into JSes and also strips flow types if you want that.
In your main scala.html file add #helper.requireJs(core = routes.WebJarAssets.at(WebJarAssets.locate("require.js")).url, module = routes.Assets.at("javascripts/main").url). This will add add a script tag with data-main and src attributes that are used to bootstrap your RequireJs app.
Create react.js file in your assets/javascripts folder:
define(['../lib/react/react-with-addons'], function(React) {
window.React = React;
return React;
});
Create main.jsx file in your assets/javascripts folder:
require.config({
// standard requirejs config here
});
require(['react', 'components/YourComponent'], function(React, YourComponent) {
'use strict';
$(document).ready(function() {
React.render(<YourComponent />, document.getElementById('container'));
});
});
Your standard React component goes to assets/javascripts/components/YourComponent.jsx and is defined like standard RequireJs module. Remember to return a React class:
define(function(require) {
var React = require('react');
var AnotherComponent = require('components/AnotherComponent');
return React.createClass({ ... });
}
I hope that helps. If you have any questions, please let me know.
Someone said to have got the text plugin working with sbt-rjs: https://groups.google.com/forum/#!topic/play-framework/F789ZzTOthc
I would attempt with the text plugin first, as it's the simplest plugin of all, right? Once this is successful, move on to the JSX plugin:
https://github.com/philix/jsx-requirejs-plugin
Have a look at: https://github.com/nemoo/democratizer
This is an example project that uses play framework, scala, slick, mysql as a restful backend.
The client is a javascript single page application (SPA) written in react. It uses react router for client site routing and ES6 javascript.
The build process combines webpack and play activator which enables simple automatic browser refresh for server and client side code.