CopyPastor

Detecting plagiarism made easy.

Score: 0.7545344233512878; Reported for: String similarity Open both answers

Possible Plagiarism

Plagiarized on 2019-07-06
by ORBIT

Original Post

Original - Posted on 2016-08-02
by swaechter



            
Present in both answers; Present only in the new answer; Present only in the old answer;

**Async Pipe & Observables**
The first approach that we'll take a look doesn't require any modification at the API level. In light of this, we'll be using the Async Pipe. Pipes in Angular work just as pipes work in Linux. They accept an input and produce an output. What the output is going to be is determined by the pipe's functionality. This pipe accepts a promise or an observable as an input, and it can update the template whenever the promise is resolved or when the observable emits some new value. As with all pipes, we need to apply the pipe in the template.
Let's assume that we have a list of products returned by an API and that we have the following service available:
// api.service.ts import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; @Injectable() export class ApiService { constructor(private http: HttpClient) { } getProducts() { return this.http.get('http://localhost:3000/api/products'); } }
The code above is straightforward - we specify the `getProducts()` method that returns the `HTTP GET` call.
It's time to consume this service in the component. And what we'll do here is create an Observable and assign the result of the `getProducts()` method to it. Furthermore, we'll make that call every 1 second, so if there's an update at the
**API level, we can refresh the template:**
// some.component.ts
import { Component, OnInit, OnDestroy, Input } from '@angular/core'; import { ApiService } from './../api.service'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/observable/interval'; import 'rxjs/add/operator/startWith'; import 'rxjs/add/operator/switchMap'; @Component({ selector: 'app-products', templateUrl: './products.component.html', styleUrls: ['./products.component.css'] }) export class ProductsComponent implements OnInit { @Input() products$: Observable<any>; constructor(private api: ApiService) { } ngOnInit() { this.products$ = Observable .interval(1000) .startWith(0).switchMap(() => this.api.getProducts()); } }
And last but not least, we need to apply the async pipe in our template:
<!-- some.component.html --> <ul> <li *ngFor="let product of products$ | async">{{ product.prod_name }} for {{ product.price | currency:'£'}}</li> </ul>
This way, if we push a new item to the API (or remove one or multiple item(s)) the updates are going to be visible in the component in 1 second.
> Note In your case

@Input() products$: Observable<any>; ngOnInit() { this.products$ = Observable .interval(1000) .startWith(0).switchMap(() => getTournament(id)); } getTournament(id: string): Observable<YourObject>{ const url = `${this.getUrl}/${id}`; //console.log(url); return this.http.get<YourObject>(url, httpJSON); }
> Html <!-- some.component.html --> <ng-container *ngFor="let item of products$ | async | paginate: { itemsPerPage: 5, currentPage: p }; let i = index"> <tr> <td>{{item.name }}</td> <!-- <td>{{item.awayTeam}}</td> <td>{{item.homeTeam}} </td> --> <td>{{item.line }}</td> <td>{{item.oddsAmerican}}</td> </tr> </ng-container> </ul>

As several people already mentioned: All files in your node_modules directory (NPM location for packages) are part of your project dependencies (So-called direct dependencies). As an addition to that, your dependencies can also have their own dependencies and so on, etc. (So-called transitive dependencies). Several ten thousand files are nothing special.
Because you are only allowed to upload 10'000 files (See comments), I would go with a bundler engine. This engine will bundle all your JavaScript, CSS, HTML, etc. and create a single bundle (or more if you specify them). Your index.html will load this bundle and that's it.
I am a fan of webpack, so my webpack solution will create an application bundle and a vendor bundle (For the full working application see here https://github.com/swaechter/project-collection/tree/master/web-angular2-example):
**index.html**
<!DOCTYPE html> <html> <head> <base href="/"> <title>Webcms</title> </head> <body> <webcms-application>Applikation wird geladen, bitte warten...</webcms-application> <script type="text/javascript" src="vendor.bundle.js"></script> <script type="text/javascript" src="main.bundle.js"></script> </body> </html>

**webpack.config.js**
var webpack = require("webpack"); var path = require('path'); var ProvidePlugin = require('webpack/lib/ProvidePlugin'); var CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin'); var UglifyJsPlugin = require('webpack/lib/optimize/UglifyJsPlugin'); /* * Configuration */ module.exports = { devtool: 'source-map', debug: true, entry: { 'main': './app/main.ts' }, // Bundle configuration output: { path: root('dist'), filename: '[name].bundle.js', sourceMapFilename: '[name].map', chunkFilename: '[id].chunk.js' }, // Include configuration resolve: { extensions: ['', '.ts', '.js', '.css', '.html'] }, // Module configuration module: { preLoaders: [ // Lint all TypeScript files {test: /\.ts$/, loader: 'tslint-loader'} ], loaders: [ // Include all TypeScript files {test: /\.ts$/, loader: 'ts-loader'}, // Include all HTML files {test: /\.html$/, loader: 'raw-loader'}, // Include all CSS files {test: /\.css$/, loader: 'raw-loader'}, ] }, // Plugin configuration plugins: [ // Bundle all third party libraries new CommonsChunkPlugin({name: 'vendor', filename: 'vendor.bundle.js', minChunks: Infinity}), // Uglify all bundles new UglifyJsPlugin({compress: {warnings: false}}), ], // Linter configuration tslint: { emitErrors: false, failOnHint: false } }; // Helper functions function root(args) { args = Array.prototype.slice.call(arguments, 0); return path.join.apply(path, [__dirname].concat(args)); }
Advantages:
- Full build line (TS linting, compiling, minification, etc.) - 3 files for deployment --> Only a few Http requests
Disadvantages:
- Higher build time - Not the best solution for Http 2 projects (See disclaimer)

**Disclaimer:** This is a good solution for Http 1.*, because it minimizes the overhead for each Http request. You only have a request for your index.html and each bundle - but not for 100 - 200 files. At the moment, this is the way to go.
Http 2, on the other hand, tries to minimize the Http overhead, so it's based on a stream protocol. This stream is able to communicate in both direction (Client <--> Server) and as a reason of that, a more intelligent resource loading is possible (You only load the required files). The stream eliminates much of the Http overhead (Less Http round trips).
But it's the same as with IPv6: It will take a few years until people will really use Http 2

        
Present in both answers; Present only in the new answer; Present only in the old answer;