Since React/vue and other frameworks have become popular, jquery has been tagged with noodle code, even "rat", as if who is still using jquery, who is still living in the old days, many people are scrambling to embrace the new framework, major blog sites have a large part of the blog is introducing a new framework, The "Surfers" in the era of competition. The new concept brought about by the new framework, the new way of development can not be denied the production efficiency, but jquery, etc. should be labeled "old-fashioned" noodle code?
We start with an article: "The introduction of React.js – for engineers who understand JQuery", the English text is this "react.js Introduction for people who Know Just enough jQuery to Get By, this article I have seen a long time ago, and now turn it out, in contrast to the next jquery and react respectively to achieve a push function, the author with jquery write code on the chaos, and with react no matter how complex the requirements, the code is still very clear.
We take a step-by-step approach to dismantling the original author's ideas.
(1) Input number is 0 o'clock, send button is not clickable
As shown, when the input box has no content, the push button is grayed out and there is a content point to point to.
The author writes the code like this:
- //Initialize State
- $ ( "button"). Prop ( "Disabled", true);
-
- //text box value changes
- $ ( " TextArea "). on ( "input", function () {
- //As long as there is more than one character, the
- if ($ (this). Val (). length > 0) {
- //button to click
Li class= "alt" > $ ( "button"). Prop ( "disabled", FALSE);
- } else {
- //Otherwise, the button cannot be clicked
- $ ( Span class= "string", "button"). Prop (true);
-
- });
The code itself is very cumbersome, first of all, since the button at the beginning is disabled, then write a disabled attribute directly on the HTML line:
- <form class="Tweet-box" >
- <textarea name="textmsg" ></textarea>
- <input disabled type="Submit" name="tweet" value="tweet" >
- </form>
The second to control the state of the button, in fact, the core as long as a line of code, do not need to write so long:
- Let form = $ (". Tweet-box") [0];
- $ (form.textmsg). On ("input", function () {
- form.tweet.disabled = this.value.length <= 0;
- }). Trigger ("input");
This code should be concise enough, and the code between jquery and the original switch back and forth, easy.
(2) To achieve the remaining word count function
As shown in the following:
This is true:
- Let form = $ (". Tweet-box") [0],
- $leftWordCount = $ ("#left-word-count");
- $ (form.textmsg). On ("input", function () {
- Number of words already available
- Let Wordscount = this.value.length;
- $leftWordCount. Text (140-wordscount);
- form.tweet.disabled = wordscount <= 0;
- });
(3) Add Picture button
As shown, the lower left corner has one more button to select a Photo:
If the user selects a photo, the input word count will be reduced by 23 characters, and the add photo copy will be converted to photo Added. Let's take a look at how the author did it, the following code:
- if ($ (this). Hasclass ("is-on")) {
- $ (This)
- . Removeclass ("is-on")
- . Text ("Add Photo");
- $ ("span"). Text ($-$ ("textarea"). Val (). length);
- } Else {
- $ (This)
- . addclass ("is-on")
- . Text ("? Photo Added ");
- $ ("span"). Text (140-23-$ ("textarea"). Val (). length);
- }
If the code is written like the author, it's really messy, and it's more noodle-style. But we can achieve it gracefully. First, selecting a photo typically writes a input[type=file] of the hidden input box cover below the upload icon:
- <div class="Upload-container">
- <img src="upload-icon.png" alt>
- <span id="Add-photo">add photo</span>
- <input type="file" name="Photoupload">
- </div>
Then listen to its Change event and set a class for the form in the Change event:
- $ (form.photoupload). On ("Change", function () {
- Add a photo-added class if you select a photo
- This.value.length? $ (form). addclass ("photo-added")
- Otherwise, remove
- : $ (form). Removeclass ("photo-added");
- });
Then you can implement the requirements of the copy change, add the above #add-photo span tag two data properties, respectively, the photo is added and the copy is not added, as shown in the following code:
- <span id="Add-photo" data-added-text="photo added"
- data-notadded-text="Add Photo" ></span>
Control the text on the HTML by combining the class of the form with the Before/after pseudo-class, as shown in the following code:
- #Add-photo:before {
- Content:attr (Data-empty-text);
- }
- form.photo-added #Add-photo:before {
- Content:attr ("Data-added-text");
- }
In this way, we are using a more elegant approach to achieve a copy of the function, which CSS attr can be compatible with the IE9, and here Html/css/js together to complete the function of this change, this should also be very fun.
The remaining requirement to lose 23 characters is just to be judged when it is lost:
- $ (form.textmsg). On ("input", function () {
- Number of words already available
- Let Wordscount = this.value.length;
- form.tweet.disabled = wordscount <= 0;
- $leftWordCount. Text (140-wordscount-
- If you've added a picture and lost 23 characters
- ($ (form). Hasclass ("photo-added")? 23:0));
- });
Then after selecting the picture trigger, let the text change, the following code is the penultimate line:
- /*
- * @trigger triggers the input event of the text input box to update the remaining word Count
- */
- $ (form.photoupload). On ("Change", function () {
- Add a photo-added class if you select a photo
- This.value.length? $ (form). addclass ("photo-added"):
- Otherwise, remove
- $ (form). Removeclass ("photo-added");
- $ (form.textmsg). Trigger ("input");
- });
Here again the use of the mechanism of the event, with the REAC should be basically used in state control.
Let's take a look at the last feature.
(4) There is no text, but there is a photo push button to be dot
Above is as long as there is no text, then push button can not point, now requires a picture to point. This is good, because if there is a picture, the form already has a class, so as long as one more judgment can be:
- $ (form.textmsg). On ("input", function () {
- Number of words already available
- Let Wordscount = this.value.length;
- form.tweet.disabled = Wordscount <= 0
- Disabled add one more and judge
- &&!$ (form). Hasclass ("photo-added");
- $leftWordCount. Text (140-wordscount-
- If you've added a picture and lost 23 characters
- ($ (form). Hasclass ("photo-added")? 23:0));
- });
Finally, a summary of the JS code, plus empty lines and comments total only 23 lines:
- Let form = $ (". Tweet-box") [0],
- $leftWordCount = $ ("#left-word-count");
- $ (form.textmsg). On ("input", function () {
- Number of words already available
- Let Wordscount = this.value.length;
- form.tweet.disabled = Wordscount <= 0
- Disabled add one more and judge
- &&!$ (form). Hasclass ("photo-added");
- $leftWordCount. Text (140-wordscount-
- If you've added a picture and lost 23 characters
- ($ (form). Hasclass ("photo-added")? 23:0));
- });
- /*
- * @trigger triggers the input event of the text input box to update the remaining word Count
- */
- $ (form.photoupload). On ("Change", function () {
- Add a photo-added class if you select a photo
- This.value.length? $ (form). addclass ("photo-added"):
- Otherwise, remove
- $ (form). Removeclass ("photo-added");
- $ (form.textmsg). Trigger ("input");
- });
There are about 10 lines of HTML and 6 lines of core CSS, but these two are easier to read. Take a look at the full version of React, the author's implementation:
- var Tweetbox = React.createclass ({
- Getinitialstate: function () {
- return {
- Text: "",
- Photoadded: false
- };
- },
- HandleChange: function (event) {
- This.setstate ({text:event.target.value});
- },
- Togglephoto: function (event) {
- This.setstate ({photoadded:!this.state.photoadded});
- },
- Remainingcharacters: function () {
- if (this.state.photoAdded) {
- return 140-23-this.state.text.length;
- } Else {
- return 140-this.state.text.length;
- }
- },
- Render: function () {
- Return (
- <div classname="Well clearfix" >
- <textarea classname="Form-control"
- Onchange={this.handlechange}></textarea>
- <br/>
- <span>{this.remainingcharacters ()}</span>
- <button classname="btn btn-primary pull-right"
- Disabled={this.state.text.length = = = 0 &&!this.state.photoadded}>tweet</button>
- <button classname="btn btn-default pull-right"
- Onclick={this.togglephoto}>
- {this.state.photoAdded? "? Photo Added ": " Add photo "}
- </button>
- </div>
- );
- }
- });
- React.render (
- <tweetbox/>
- Document.body
- );
React's routine is to listen for events and then change state, and in Jsx's template, use these state displays, while jquery is listening to events and then controlling the DOM display on its own. React help you operate the Dom,jquery to operate the DOM itself, which provides convenience but also loses flexibility, which adds flexibility but adds complexity.
Using jquery many people easy to write the noodle-style code, but the style of writing code I think and the framework does not matter, the key is your coding quality, as you use the react write class, you can say you are object-oriented? Not necessarily, I mentioned in the "JS and object-oriented" article, Writing class does not mean that you are object-oriented, object-oriented is an idea rather than the organization of your code. Once you're out of the react frame, are you going back to the style of the spaghetti code? If so, it means that you haven't mastered the object-oriented thinking. However, it is undeniable that frameworks such as react can be easily modular.
Another thing to note is that the framework will help you block out many of the native details and allow you to focus on the business logic, but often it also gives you the ability to lose your native abilities, whether it's HTML or JS, and that's the most important foundation of your life. For example, for an event, because all events are tied directly to the target element and then passed through the state or other third-party framework, there is actually no concept of event. So need to be wary of using the framework but lost the basic front-end capabilities, such as Ajax paging change URL, or the implementation of single-page routing, as well as the control of the front back, basically can be completely answered less. Many people will use the framework to do the page, but do not understand JS.
JQuery = = = Noodle code?