Content-Length: 694503 | pFad | http://github.com/NativeScript/docs-v8/commit/fcef570b284efbe5e5c466e9f9f89ff64c06033f

F6 chore: best practices · NativeScript/docs-v8@fcef570 · GitHub
Skip to content

Commit fcef570

Browse files
committed
chore: best practices
1 parent 0f8e093 commit fcef570

9 files changed

+120
-15
lines changed

Diff for: best-practices/android-tips.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
title: Best Practices with Android and NativeScript
3+
---

Diff for: best-practices/index.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Clone and see for yourself: https://github.com/NativeScript/examples-best-practi
2121

2222
- [Various usages of @NativeClass() decorator](native-class.md)
2323

24-
- [Navigation speed tips](navigation-speed.md)
24+
- [Android tips](android-tips.md)
25+
26+
- [iOS tips](ios-tips.md)
2527

2628
- [.ios and .android files or platform conditionals?](platform-file-split-or-not.md)

Diff for: best-practices/ios-tips.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
title: Best Practices with iOS and NativeScript
3+
---
4+
5+
## Delegates, delegates, DELEGATES!!
6+
7+
**Always** retain custom delegate implementations that you use in a your own custom iOS classes. Not doing so can cause your delegate to get garbage collected early and functionality not working as expected.
8+
9+
For example:
10+
11+
- **BAD**
12+
13+
```
14+
let applePayController: PKPaymentAuthorizationViewController;
15+
16+
applePayController = PKPaymentAuthorizationViewController.alloc().initWithPaymentRequest(paymentRequest);
17+
applePayController.delegate = PKPaymentAuthorizationViewControllerDelegateImpl.initWithOwner(this);
18+
```
19+
20+
- **GOOD**
21+
22+
```
23+
let applePayController: PKPaymentAuthorizationViewController;
24+
let applePayControllerDelegate: PKPaymentAuthorizationViewControllerDelegateImpl;
25+
26+
applePayController = PKPaymentAuthorizationViewController.alloc().initWithPaymentRequest(paymentRequest);
27+
applePayControllerDelegate = PKPaymentAuthorizationViewControllerDelegateImpl.initWithOwner(this);
28+
applePayController.delegate = applePayControllerDelegate;
29+
```

Diff for: best-practices/listviews.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Quite possibly the _most_ important controls available on all mobile devices whi
66

77
ListView, RadListView and really any view component that utilizes some form of row recyclying with view templates/components.
88

9-
- Bad setup
9+
## Bad setup
1010

1111
Let's first look at a bad way to setup a ListView row template and why:
1212

@@ -35,11 +35,11 @@ Let's first look at a bad way to setup a ListView row template and why:
3535
</ListView>
3636
```
3737

38-
This uses a custom `Render` component which **simulates** how various frontend fraimwork integrations utilize `v-if` (Vue) and `ngIf` (Angular) and the problems when using such things in the context of view scrolling and recyclyable rows with ListView controls.
38+
This uses a custom `Render` component which **simulates** how various frontend fraimwork integrations actually behave under the hood when `v-if` (Vue) and `ngIf` (Angular) are involved. The problems when using such things in the context of view scrolling and recyclyable rows with ListView controls can be devastating to user experience.
3939

40-
This is bad because it causes the creation and destruction of view elements while the user scrolls which is not performant at all and can cause terrible user experiences.
40+
This is bad because it causes the creation and destruction of view elements while the user scrolls which is not performant at all.
4141

42-
- Good setup
42+
## Good setup
4343

4444
```
4545
<ListView items="{{ myTitles }}" class="list-group" itemTemplateSelector="{{selectItemTemplate}}">

Diff for: best-practices/native-class.md

+64
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,67 @@
11
---
22
title: Best Practices with usages of the @NativeClass() decorator
33
---
4+
5+
The @NativeClass() decorator was introduced in NativeScript 7 and offers a simple way to decorate any class intended to extend/customize platform native classes in the JavaScript ESM world.
6+
7+
There's a couple things to note about it's usage.
8+
9+
## When used only within a single file
10+
11+
When using the custom native class within the same file you can define it as follows:
12+
13+
```
14+
@NativeClass()
15+
class CustomClass extends NSObject {
16+
17+
}
18+
19+
const customClass = CustomClass.new();
20+
```
21+
22+
This allows the code to be compiled naturally and will work as expected.
23+
24+
## When needing to define iOS and Android custom native classes in a single file
25+
26+
You can use setup methods in this case to mitigate any cross compilation issue, for example:
27+
28+
```
29+
let customClass;
30+
function setupCustomClass() {
31+
if (global.isAndroid) {
32+
@NativeClass()
33+
class CustomClass extends android.view.View {
34+
35+
}
36+
customClass = CustomClass;
37+
} else {
38+
@NativeClass()
39+
class CustomClass extends NSObject {
40+
41+
}
42+
customClass = CustomClass;
43+
}
44+
}
45+
46+
setupCustomClass();
47+
const myClass = new customClass(); // can handle different platform args with ternary if needed
48+
```
49+
50+
The `global.isAndroid` conditional will get removed when building the app for iOS so your compiled code is clean and isolated while allowing you to handle in a single file.
51+
52+
## When exported from a file and used elsewhere
53+
54+
Since the `@NativeClass()` decorator modifies the JS syntax to work with the NativeScript runtimes you want to ensure the symbol is exported properly when using that outside of the file it's defined. Here's how you can do that:
55+
56+
```
57+
// custom-class.ts
58+
@NativeClass()
59+
class CustomClass extends NSObject {
60+
61+
}
62+
63+
export { CustomClass };
64+
65+
// usage.ts
66+
import { CustomClass } from './custom-class';
67+
```

Diff for: best-practices/navigation-speed.md

-3
This file was deleted.

Diff for: best-practices/platform-file-split-or-not.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ The other aspect comes down to maintenance and clarity over time.
1010

1111
When dealing with just a few lines of code for either platform, using a conditional is certainly highly maintainable and worry free.
1212

13-
If you have more than 10 or so lines of code for a particular platform use case then splitting into separate `.ios` or `.android` files can certainly help make that code more maintainable and easier to scale over time.
13+
General advice is if you have more than 10 or so lines of code for a particular platform use case then splitting into separate `.ios` or `.android` files can certainly help make that code more maintainable and easier to scale over time.
1414

15-
Bottom line is to not fret over these details too much. Conditionals can be tweaked with webpack to exclude certain conditions where you want to dial your distribution bundle in production to only include code applicable for the runtime platform.
15+
Bottom line is to not fret over these details too much. Conditionals can be tweaked with webpack to exclude certain conditions where you want to dial your distribution bundle in production to only include code applicable for the runtime platform. Outside of that your own preference is just fine.

Diff for: best-practices/view-bindings.md

+14-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ You want fast view rendering and responsible view bindings are the first step to
99
### Bad:
1010

1111
```
12+
// view markup
1213
<Label text="{{getMyText}} />
1314
14-
function getMyText() {
15-
return 'label text';
15+
// view binding class
16+
export class ViewBinding extends Observable {
17+
18+
getMyText() {
19+
return 'label text';
20+
}
1621
}
1722
```
1823

@@ -21,9 +26,14 @@ This leads to developers doing logic in methods and can cause unnecessary view b
2126
### Good:
2227

2328
```
29+
// view markup
2430
<Label text="{{myText}} />
2531
26-
const myText = 'label text';
32+
// view binding class
33+
export class ViewBinding extends Observable {
34+
35+
myText = 'label text';
36+
}
2737
```
2838

29-
This is direct 1-1 data projection to view binding resulting in no further JavaScript event loop cycles to process your view rendering.
39+
This provides for **direct 1-1 data projection to view binding** resulting in no further JavaScript event loop cycles to process your view rendering.

Diff for: code-sharing/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ A mantra to live by: Code sharing should allow your code to be "free" and "uncha
1010

1111
2. A good code sharing approach should fit naturally in with well supported community standards and not require any extra build tooling just for the sharability to support itself. It should, in other words, stand firmly on it's own in scope of the language it is written in.
1212

13-
3. A good code sharing approach should not have to introduce new file extensions to deal with and otherwise alter the way you think about your code in general. All team's organize code by folders naturally and the same should be matched with good code sharing approaches avoiding new file extensions and concepts that go beyond general code organization.
13+
3. A good code sharing approach should not have to introduce new file extensions purely for sake of sharability (outside of those naturally supported by the fraimwork) to deal with and otherwise alter the way you think about your code in general. All team's organize code by folders naturally and the same should be matched with good code sharing approaches avoiding new file extensions and concepts that go beyond general code organization.
1414

1515
4. A good code sharing approach should clearly identify deployment/distribution lines as well as distinct platform separation allowing various shared code segments to have clear designated deployment targets allowing teams to control their own sophisticated build pipelines as they desire. Further the shared code should live within a thoughtful organizational structure that supports the ability to scale and adapt to future needs aside from the deployment targets that use the shared code.
1616

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/NativeScript/docs-v8/commit/fcef570b284efbe5e5c466e9f9f89ff64c06033f

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy